fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. enum State {
  6. WhiteSpace,
  7. Block,
  8. String };
  9.  
  10. static string Parse(string s) {
  11. State state = State.WhiteSpace;
  12. int? blockStart = null;
  13. for (int p = 0; ; ) {
  14. switch (state) {
  15. case State.WhiteSpace:
  16. if (s[p] == '{') {
  17. p++;
  18. state = State.Block;
  19. blockStart = p;
  20. } else if (s[p] == ' ') {
  21. p++;
  22. } else {
  23. throw new System.ArgumentException();
  24. }
  25. break;
  26. case State.Block:
  27. if (s[p] == '}') {
  28. return s.Substring((int) blockStart, p - (int) blockStart);
  29. } else if (s[p] == '"') {
  30. p++;
  31. state = State.String;
  32. } else {
  33. p++;
  34. }
  35. break;
  36. case State.String:
  37. if (s[p] == '"') {
  38. p++;
  39. state = State.Block;
  40. } else {
  41. p++;
  42. }
  43. break;
  44. }
  45. }
  46. throw new System.ArgumentException();
  47. }
  48.  
  49. public static void Main()
  50. {
  51. Console.Out.WriteLine(Parse("{ --isso é um bloco;\n echo \"Aqui tem um } no meio\";}"));
  52. }
  53. }
Success #stdin #stdout 0.03s 24072KB
stdin
Standard input is empty
stdout
 --isso é um bloco;
  echo "Aqui tem um } no meio";