StringHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // StringHelper.cs
  2. // Author: Sergey Chaban ([email protected])
  3. using System;
  4. using System.Text;
  5. namespace Mono.ILASM {
  6. /// <summary>
  7. /// </summary>
  8. internal class StringHelper : StringHelperBase {
  9. private static readonly string idChars = "_$@?`";
  10. /// <summary>
  11. /// </summary>
  12. /// <param name="host"></param>
  13. public StringHelper (ILTokenizer host) : base (host)
  14. {
  15. }
  16. /// <summary>
  17. /// </summary>
  18. /// <returns></returns>
  19. public override bool Start (char ch)
  20. {
  21. mode = Token.UNKNOWN;
  22. if (Char.IsLetter (ch) || idChars.IndexOf (ch) != -1) {
  23. mode = Token.ID;
  24. } else if (ch == '\'') {
  25. mode = Token.SQSTRING;
  26. } else if (ch == '"') {
  27. mode = Token.QSTRING;
  28. }
  29. return (mode != Token.UNKNOWN);
  30. }
  31. private static bool IsIdChar (int c)
  32. {
  33. char ch = (char) c;
  34. return (Char.IsLetterOrDigit(ch) || idChars.IndexOf (ch) != -1);
  35. }
  36. /// <summary>
  37. /// </summary>
  38. /// <returns></returns>
  39. public override string Build ()
  40. {
  41. if (mode == Token.UNKNOWN) return String.Empty;
  42. int ch = 0;
  43. ILReader reader = host.Reader;
  44. StringBuilder idsb = new StringBuilder ();
  45. if (mode == Token.SQSTRING || mode == Token.QSTRING) {
  46. int term = (mode == Token.SQSTRING) ? '\'' : '"';
  47. reader.Read (); // skip quote
  48. for (ch = reader.Read (); ch != -1; ch = reader.Read ()) {
  49. if (ch == term) {
  50. break;
  51. }
  52. if (ch == '\\') {
  53. ch = reader.Read ();
  54. /*
  55. * Long string can be broken across multiple lines
  56. * by using '\' as the last char in line.
  57. * Any white space chars between '\' and the first
  58. * char on the next line are ignored.
  59. */
  60. if (ch == '\n') {
  61. reader.SkipWhitespace ();
  62. continue;
  63. }
  64. int escaped = Escape (ch);
  65. if (escaped == -1) {
  66. reader.Unread (ch);
  67. ch = '\\';
  68. } else {
  69. ch = escaped;
  70. }
  71. }
  72. idsb.Append((char)ch);
  73. }
  74. } else { // ID
  75. while ((ch = reader.Read ()) != -1) {
  76. if (IsIdChar (ch)) {
  77. idsb.Append ((char) ch);
  78. } else {
  79. reader.Unread (ch);
  80. break;
  81. }
  82. }
  83. }
  84. return idsb.ToString ();
  85. }
  86. /// <summary>
  87. /// </summary>
  88. /// <param name="ch"></param>
  89. /// <returns></returns>
  90. public static int Escape (int ch)
  91. {
  92. int res = -1;
  93. if (ch >= '0' && ch <='7') {
  94. //TODO : octal code
  95. } else {
  96. int id = "abfnrtv\"'\\".IndexOf ((char)ch);
  97. if (id != -1) {
  98. res = "\a\b\f\n\r\t\v\"'\\" [id];
  99. }
  100. }
  101. return res;
  102. }
  103. }
  104. }