Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, you are obviously using some kind of parsing library, you would have better chance if you had modified your code, e.g. to something like I did, so that anybody can copy, paste, run your code.</p> <p>Answer is simple, your (string literal)-parsing region does not parse all input. Here is your code modified to be used without any additional library:</p> <pre><code>public class Test { static char ElementAtOrDefault(string value, int position) { return position &gt;= value.Length ? default(char) : value[position]; } static string parseStringLiteral(string value, ref int ChPosition) { StringBuilder Value = new StringBuilder(); char ChCurrent = ElementAtOrDefault(value, ++ChPosition); while (ChCurrent != '"') { Value.Append(ChCurrent); ChCurrent = ElementAtOrDefault(value, ++ChPosition); if (ChCurrent == '"') { // "" sequence only acceptable if (ElementAtOrDefault(value, ChPosition + 1) == '"') { Value.Append(ChCurrent); // skip 2nd double quote ChPosition++; // move position next ChCurrent = ElementAtOrDefault(value, ++ChPosition); } } else if (default(Char) == ChCurrent) { // message: unterminated string throw new Exception("ScanningException"); } } ChPosition++; return Value.ToString(); } public static void test(string literal) { Console.WriteLine("testing literal with " + literal.Length + " chars:\n" + literal); try { int pos = 0; string res = parseStringLiteral(literal, ref pos); Console.WriteLine("Parsed " + res.Length + " chars:\n" + res); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } Console.WriteLine(); } public static int Main(string[] args) { test(@"""Hello Language Design"""); test(@"""Is there any problems with the """"strings""""?"""); test(@"""v#:';?325;.&lt;&gt;,|+_)""(*&amp;^%$#@![]{}\|-_="""); return 0; } } </code></pre> <p>Running this program produces output:</p> <pre>testing literal with 23 chars: "Hello Language Design" Parsed 21 chars: Hello Language Design testing literal with 45 chars: "Is there any problems with the ""strings""?" Parsed 41 chars: Is there any problems with the "strings"? testing literal with 39 chars: "v#:';?325;.,|+_)"(*&^%$#@![]{}\|-_=" Parsed 18 chars: v#:';?325;.,|+_)</pre> <p>So it works for your testing, but algorithm is not correct, try running:</p> <pre><code>//literal with "", should produce ", but it does not test(@""""""""""); </code></pre> <p>And you will incorrectly get:</p> <pre>testing literal with 4 chars: """" Parsed 0 chars:</pre> <p>Problem is, if you encounter character " in your while condition, you do not check next character, if it is " or not:</p> <pre><code>while (ChCurrent != '"') //bug </code></pre> <p>Of course, I created correct version for you :-) Here it is (it uses your style, just edited version of yours):</p> <pre><code>static string parseStringLiteral(string value, ref int ChPosition) { StringBuilder Value = new StringBuilder(); char ChCurrent = ElementAtOrDefault(value, ++ChPosition); bool goon = true; while (goon) { if (ChCurrent == '"') { // "" sequence only acceptable if (ElementAtOrDefault(value, ChPosition + 1) == '"') { Value.Append(ChCurrent); // skip 2nd double quote ChPosition++; // move position next ChCurrent = ElementAtOrDefault(value, ++ChPosition); } else goon = false; //break; } else if (default(Char) == ChCurrent) { // message: unterminated string throw new Exception("ScanningException"); } else { Value.Append(ChCurrent); ChCurrent = ElementAtOrDefault(value, ++ChPosition); } } ChPosition++; return Value.ToString(); } </code></pre> <p>Happy coding :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload