Open-Source-Software-Entwicklung und Downloads

Browse Subversion Repository

Contents of /trunk/TTProxy/YCL/include/YCL/StringUtil.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (show annotations) (download) (as text)
Sat Aug 18 08:52:18 2007 UTC (16 years, 9 months ago) by maya
Original Path: TTProxy/trunk/YCL/include/YCL/StringUtil.h
File MIME type: text/x-chdr
File size: 4518 byte(s)
$Log$ を削除した。

1 /*
2 * $Id: StringUtil.h,v 1.4 2007-08-18 08:52:18 maya Exp $
3 */
4
5 #ifndef _YCL_STRINGUTIL_H_
6 #define _YCL_STRINGUTIL_H_
7
8 #if _MSC_VER >= 1000
9 #pragma once
10 #endif // _MSC_VER >= 1000
11
12 #include <YCL/common.h>
13
14 #include <YCL/String.h>
15 #include <YCL/StringBuffer.h>
16
17 namespace yebisuya {
18
19 class StringUtil {
20 private:
21 static int hexadecimal(char c) {
22 if ('0' <= c && c <= '9') {
23 return c - '0';
24 }else if ('A' <= c && c <= 'F') {
25 return c - 'A' + 10;
26 }else if ('a' <= c && c <= 'f') {
27 return c - 'a' + 10;
28 }else{
29 return -1;
30 }
31 }
32 static int octet(char c) {
33 return '0' <= c && c <= '7' ? c - '0' : -1;
34 }
35 static const char* ESCAPE_CHARS() {
36 static const char string[] = "abfnrtv";
37 return string;
38 }
39 static const char* CONTROL_CHARS() {
40 static const char string[] = "\a\b\f\n\r\t\v";
41 return string;
42 }
43 public:
44 static String escape(String string) {
45 if (string == NULL)
46 return NULL;
47 return escape(string, 0, string.length());
48 }
49 static String escape(String string, int offset, int length) {
50 if (string == NULL)
51 return NULL;
52 StringBuffer buffer;
53 return escape(buffer, string, offset, length) ? buffer.toString() : string;
54 }
55 static bool escape(StringBuffer& buffer, String string) {
56 if (string == NULL)
57 return false;
58 return escape(buffer, string, 0, string.length());
59 }
60 static bool escape(StringBuffer& buffer, String string, int offset, int length) {
61 if (string == NULL)
62 return false;
63 const char* start = string;
64 start += offset;
65 const char* p = start;
66 const char* end = start + length;
67 while (p < end && *p != '\0') {
68 char ch = *p;
69 if ('\0' < ch && ch < ' ' || ch == '\'' || ch == '"' || ch == '?' || ch == '\\') {
70 bool useOctet;
71 if (ch < ' ') {
72 useOctet = true;
73 for (int index = 0; CONTROL_CHARS()[index] != '\0'; index++) {
74 if (ch == CONTROL_CHARS()[index]) {
75 ch = ESCAPE_CHARS()[index];
76 useOctet = false;
77 break;
78 }
79 }
80 }else{
81 useOctet = false;
82 }
83 if (p > start) {
84 buffer.append(start, p - start);
85 }
86 buffer.append('\\');
87 if (useOctet) {
88 int octet = 6;
89 while (octet >= 0) {
90 buffer.append((char) ('0' + ((ch >> octet) & 0x07)));
91 octet -= 3;
92 }
93 }else{
94 buffer.append(ch);
95 }
96 start = ++p;
97 }else{
98 if (String::isLeadByte(ch))
99 p++;
100 p++;
101 }
102 }
103 if (start == (const char*) string) {
104 return false;
105 }
106 buffer.append(start);
107 return true;
108 }
109
110 static String unescape(String string) {
111 if (string == NULL)
112 return NULL;
113 return unescape(string, 0, string.length());
114 }
115 static String unescape(String string, int offset, int length) {
116 if (string == NULL)
117 return NULL;
118 StringBuffer buffer;
119 return unescape(buffer, string) ? buffer.toString() : string;
120 }
121 static bool unescape(StringBuffer& buffer, String string) {
122 if (string == NULL)
123 return false;
124 return unescape(buffer, string, 0, string.length());
125 }
126 static bool unescape(StringBuffer& buffer, String string, int offset, int length) {
127 if (string == NULL)
128 return false;
129 const char* start = string;
130 start += offset;
131 const char* p = start;
132 const char* end = start + length;
133 while (p < end && *p != '\0') {
134 if (*p == '\\') {
135 if (p > start) {
136 buffer.append(start, p - start);
137 start = p;
138 }
139 char ch = '\0';
140 switch (*++p) {
141 case '\'':
142 case '"':
143 case '\\':
144 case '?':
145 ch = *p++;
146 break;
147 case 'x':
148 case 'X':
149 {
150 int ch1 = hexadecimal(*++p);
151 if (ch1 != -1) {
152 int ch2 = hexadecimal(*++p);
153 if (ch2 != -1) {
154 p++;
155 ch = (char) (ch1 << 4 | ch2);
156 }else{
157 ch = (char) ch1;
158 }
159 }
160 }
161 break;
162 case '0': case '1': case '2': case '3':
163 case '4': case '5': case '6': case '7':
164 {
165 ch = octet(*p);
166 int d = octet(*++p);
167 if (d != -1) {
168 ch = (ch << 3 | d);
169 d = octet(*++p);
170 if (d != -1) {
171 ch = (ch << 3 | d);
172 p++;
173 }
174 }
175 }
176 break;
177 default:
178 for (int index = 0; ESCAPE_CHARS()[index] != '\0'; index++) {
179 if (*p == ESCAPE_CHARS()[index]) {
180 ch = CONTROL_CHARS()[index];
181 p++;
182 break;
183 }
184 }
185 }
186 if (ch != '\0') {
187 buffer.append(ch);
188 start = p;
189 }
190 }else{
191 if (String::isLeadByte(*p)) {
192 p++;
193 }
194 p++;
195 }
196 }
197 if (start == (const char*) string) {
198 return false;
199 }
200 buffer.append(start);
201 return true;
202 }
203 };
204
205 }
206
207 #endif//_YCL_STRINGUTIL_H_

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26