File: StringHelper.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (142 lines) | stat: -rw-r--r-- 3,215 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// StringHelper.cs
// Author: Sergey Chaban (serge@wildwestsoftware.com)

using System;
using System.Text;

namespace Mono.ILASM {

	/// <summary>
	/// </summary>
	internal class StringHelper : StringHelperBase {

		private static readonly string startIdChars = "#$@_";
		private static readonly string idChars = "_$@?`";

		/// <summary>
		/// </summary>
		/// <param name="host"></param>
		public StringHelper (ILTokenizer host) : base (host)
		{
		}


		/// <summary>
		/// </summary>
		/// <returns></returns>
		public override bool Start (char ch)
		{
			mode = Token.UNKNOWN;

			if (Char.IsLetter (ch) || startIdChars.IndexOf (ch) != -1) {
				mode = Token.ID;
			} else if (ch == '\'') {
				mode = Token.SQSTRING;
			} else if (ch == '"') {
				mode = Token.QSTRING;
			}

			return (mode != Token.UNKNOWN);
		}


		private static bool IsIdChar (int c)
		{
			char ch = (char) c;
			return (Char.IsLetterOrDigit(ch) || idChars.IndexOf (ch) != -1);
		}

		/// <summary>
		/// </summary>
		/// <returns></returns>
		public override string Build ()
		{
			if (mode == Token.UNKNOWN) return String.Empty;
			int ch = 0;

			ILReader reader = host.Reader;

			StringBuilder idsb = new StringBuilder ();
			if (mode == Token.SQSTRING || mode == Token.QSTRING) {
				int term = (mode == Token.SQSTRING) ? '\'' : '"';
				reader.Read (); // skip quote
				for (ch = reader.Read (); ch != -1; ch = reader.Read ()) {
					if (ch == term) {
						break;
					}

					if (ch == '\\') {
						ch = reader.Read ();

						/*
						 * Long string can be broken across multiple lines
						 * by using '\' as the last char in line.
						 * Any white space chars between '\' and the first
						 * char on the next line are ignored.
						 */
						if (ch == '\n') {
							reader.SkipWhitespace ();
							continue;
						}

						int escaped = Escape (reader, ch);
						if (escaped == -1) {
                                                        reader.Unread (ch);
                                                        ch = '\\';
                                                } else {
                                                        ch = escaped;
						}
					}

					idsb.Append((char)ch);
				}
			} else { // ID
				while ((ch = reader.Read ()) != -1) {
					if (IsIdChar (ch)) {
						idsb.Append ((char) ch);
					} else {
						reader.Unread (ch);
						break;
					}
				}
			}
			return idsb.ToString ();
		}




		/// <summary>
		/// </summary>
		/// <param name="ch"></param>
		/// <returns></returns>
		public static int Escape (ILReader reader, int ch)
		{
			int res = -1;

			if (ch >= '0' && ch <='7') {
				StringBuilder octal = new StringBuilder ();
				octal.Append ((char)ch);
				int possibleOctalChar = reader.Peek ();
				if (possibleOctalChar >= '0' && possibleOctalChar <='7') {
					octal.Append ((char)reader.Read ());
					possibleOctalChar = reader.Peek ();
					if (possibleOctalChar >= '0' && possibleOctalChar <='7')
						octal.Append ((char)reader.Read ());
				}
				res = Convert.ToInt32(octal.ToString (), 8);
			} else {
				int id = "abfnrtv\"'\\".IndexOf ((char)ch);
				if (id != -1) {
					res = "\a\b\f\n\r\t\v\"'\\" [id];
				}
			}

			return res;
		}

	}


}