2 // Author: Sergey Chaban (serge@wildwestsoftware.com)
11 internal class StringHelper
: StringHelperBase
{
13 private static readonly string startIdChars
= "#$@_";
14 private static readonly string idChars
= "_$@?`";
18 /// <param name="host"></param>
19 public StringHelper (ILTokenizer host
) : base (host
)
26 /// <returns></returns>
27 public override bool Start (char ch
)
31 if (Char
.IsLetter (ch
) || startIdChars
.IndexOf (ch
) != -1) {
33 } else if (ch
== '\'') {
34 mode
= Token
.SQSTRING
;
35 } else if (ch
== '"') {
39 return (mode
!= Token
.UNKNOWN
);
43 private static bool IsIdChar (int c
)
46 return (Char
.IsLetterOrDigit(ch
) || idChars
.IndexOf (ch
) != -1);
51 /// <returns></returns>
52 public override string Build ()
54 if (mode
== Token
.UNKNOWN
) return String
.Empty
;
57 ILReader reader
= host
.Reader
;
59 StringBuilder idsb
= new StringBuilder ();
60 if (mode
== Token
.SQSTRING
|| mode
== Token
.QSTRING
) {
61 int term
= (mode
== Token
.SQSTRING
) ? '\'' : '"';
62 reader
.Read (); // skip quote
63 for (ch
= reader
.Read (); ch
!= -1; ch
= reader
.Read ()) {
72 * Long string can be broken across multiple lines
73 * by using '\' as the last char in line.
74 * Any white space chars between '\' and the first
75 * char on the next line are ignored.
78 reader
.SkipWhitespace ();
82 int escaped
= Escape (reader
, ch
);
91 idsb
.Append((char)ch
);
94 while ((ch
= reader
.Read ()) != -1) {
96 idsb
.Append ((char) ch
);
103 return idsb
.ToString ();
111 /// <param name="ch"></param>
112 /// <returns></returns>
113 public static int Escape (ILReader reader
, int ch
)
117 if (ch
>= '0' && ch
<='7') {
118 StringBuilder octal
= new StringBuilder ();
119 octal
.Append ((char)ch
);
120 int possibleOctalChar
= reader
.Peek ();
121 if (possibleOctalChar
>= '0' && possibleOctalChar
<='7') {
122 octal
.Append ((char)reader
.Read ());
123 possibleOctalChar
= reader
.Peek ();
124 if (possibleOctalChar
>= '0' && possibleOctalChar
<='7')
125 octal
.Append ((char)reader
.Read ());
127 res
= Convert
.ToInt32(octal
.ToString (), 8);
129 int id
= "abfnrtv\"'\\".IndexOf ((char)ch
);
131 res
= "\a\b\f\n\r\t\v\"'\\" [id
];