fork download
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var input = "joe \\\"dudes\\\"\"";
  10. Console.WriteLine(input);
  11. Console.WriteLine(ReadSingleLineStringLiteral(input));
  12. }
  13.  
  14. private static string ReadSingleLineStringLiteral(string line)
  15. {
  16. using (var reader = new StringReader(line))
  17. {
  18. return ReadSingleLineStringLiteral(reader);
  19. }
  20. }
  21.  
  22. private static string ReadSingleLineStringLiteral(TextReader reader)
  23. {
  24. StringBuilder buffer = new StringBuilder ();
  25. int nextChar = reader.Read ();
  26. while (nextChar != -1) {
  27. buffer.Append ((char)nextChar);
  28. if (nextChar == '"' &&
  29. (buffer.Length == 1 ||
  30. buffer [buffer.Length - 1] != '\\' ||
  31. (buffer.Length - buffer.ToString ().TrimEnd ('\\').Length) % 2 == 0)) {
  32. break;
  33. }
  34. nextChar = reader.Read ();
  35. }
  36. if (buffer [buffer.Length - 1] != '"')
  37. throw new ArgumentException ("unclosed string literal in hyperlisp file");
  38. return buffer.ToString ().Substring (0, buffer.Length - 1)
  39. .Replace ("\n", "\r\n") // normalizing carriage returns
  40. .Replace ("\r\r\n", "\r\n")
  41. .Replace ("\\\"", "\"")
  42. .Replace ("\\\\", "\\");
  43. }
  44. }
Success #stdin #stdout 0.02s 34752KB
stdin
Standard input is empty
stdout
joe \"dudes\""
joe \