/// <summary> /// Extract a string from a quoted string, allows for doubling the quotes /// for example 'o''clock' /// </summary> /// <param name="line">Line to extract from (with extra info)</param> /// <param name="quoteChar">Quote char to remove</param> /// <param name="allowMultiline">can we have a new line in middle of string</param> /// <returns>Extracted information</returns> internal static ExtractedInfo ExtractQuotedString(LineInfo line, char quoteChar, bool allowMultiline) { // if (line.mReader == null) // throw new BadUsageException("The reader can´t be null"); if (line.IsEOL()) { throw new BadUsageException( "An empty String found. This can not be parsed like a QuotedString try to use SafeExtractQuotedString"); } if (line.mLineStr[line.mCurrentPos] != quoteChar) { throw new BadUsageException("The source string does not begin with the quote char: " + quoteChar); } var res = new StringBuilder(32); bool firstFound = false; int i = line.mCurrentPos + 1; //bool mustContinue = true; while (line.mLineStr != null) { while (i < line.mLineStr.Length) { if (line.mLineStr[i] == quoteChar) { if (firstFound) { // Is an escaped quoted char res.Append(quoteChar); firstFound = false; } else { firstFound = true; } } else { if (firstFound) { // This was the end of the string line.mCurrentPos = i; return(new ExtractedInfo(res.ToString())); } else { res.Append(line.mLineStr[i]); } } i++; } if (firstFound) { line.mCurrentPos = i; return(new ExtractedInfo(res.ToString())); } else { if (allowMultiline == false) { throw new BadUsageException("The current field has an unclosed quoted string. Complete line: " + res.ToString()); } line.ReadNextLine(); res.Append(NewLine); //lines++; i = 0; } } throw new BadUsageException("The current field has an unclosed quoted string. Complete Filed String: " + res.ToString()); }