]>
Dogcows Code - chaz/yoink/blob - string.cc
61bbf8fa6e77982a92b2c4fdf1542a24ab96a42b
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
14 #include <boost/shared_array.hpp>
16 #include "ConvertUTF.h"
24 wstring
multi_to_wide(const string
& multi
)
26 typedef boost::shared_array
<wchar_t> buffer
;
28 if (sizeof(wchar_t) == 2)
30 size_t length
= multi
.length();
31 buffer
wide(new wchar_t[length
+ 1]);
32 const UTF8
* src1
= (const UTF8
*)multi
.c_str();
33 const UTF8
* src2
= src1
+ length
;
34 UTF16
* dst1
= (UTF16
*)wide
.get();
35 UTF16
* dst2
= dst1
+ length
+1;
37 if (ConvertUTF8toUTF16(&src1
, src2
,
38 &dst1
, dst2
, lenientConversion
) != conversionOK
)
40 throw std::runtime_error("bad string conversion");
44 wstring
str(wide
.get());
47 else if (sizeof(wchar_t) == 4)
49 size_t length
= multi
.length();
50 buffer
wide(new wchar_t[length
+ 1]);
51 const UTF8
* src1
= (const UTF8
*)multi
.c_str();
52 const UTF8
* src2
= src1
+ length
;
53 UTF32
* dst1
= (UTF32
*)wide
.get();
54 UTF32
* dst2
= dst1
+ length
+1;
56 if (ConvertUTF8toUTF32(&src1
, src2
,
57 &dst1
, dst2
, lenientConversion
) != conversionOK
)
59 throw std::runtime_error("bad string conversion");
63 wstring
str(wide
.get());
68 throw std::runtime_error("unknown size of wide characters");
72 string
wide_to_multi(const wstring
& wide
)
74 typedef boost::shared_array
<char> buffer
;
76 if (sizeof(wchar_t) == 2)
78 size_t length
= wide
.length();
79 size_t multi_length
= 3 * length
+ 1;
80 buffer
multi(new char[multi_length
]);
81 const UTF16
* src1
= (const UTF16
*)wide
.c_str();
82 const UTF16
* src2
= src1
+ length
;
83 UTF8
* dst1
= (UTF8
*)multi
.get();
84 UTF8
* dst2
= dst1
+ multi_length
;
86 if (ConvertUTF16toUTF8(&src1
, src2
,
87 &dst1
, dst2
, lenientConversion
) != conversionOK
)
89 throw std::runtime_error("bad string conversion");
93 string
str(multi
.get());
96 else if (sizeof(wchar_t) == 4)
98 size_t length
= wide
.length();
99 size_t multi_length
= 4 * length
+ 1;
100 buffer
multi(new char[multi_length
]);
101 const UTF32
* src1
= (const UTF32
*)wide
.c_str();
102 const UTF32
* src2
= src1
+ length
;
103 UTF8
* dst1
= (UTF8
*)multi
.get();
104 UTF8
* dst2
= dst1
+ multi_length
;
106 if (ConvertUTF32toUTF8(&src1
, src2
,
107 &dst1
, dst2
, lenientConversion
) != conversionOK
)
109 throw std::runtime_error("bad string conversion");
113 string
str(multi
.get());
118 throw std::runtime_error("unknown size of wide characters");
123 static script
& regex_script()
125 static script script
;
126 static bool init
= true;
129 script
.import_string_library();
130 script
.globals().push_field("string").push_field("match");
131 script
.globals().set_field("match");
132 script
.top().push_field("gmatch");
133 script
.globals().set_field("gmatch");
134 script
.top().push_field("gsub");
135 script
.globals().set_field("gsub");
137 script
.globals().set_field("string");
145 regex::regex(const string
& pattern
)
147 regex::pattern(pattern
);
150 regex::regex(const string
& pattern
, const string
& source
)
152 regex::pattern(pattern
);
158 script
& script
= regex_script();
160 script
.push_pointer(this);
162 script
.globals().set_field();
163 script
.push_pointer(this);
165 script
.registry().set_field();
169 string
regex::pattern() const
171 script
& script
= regex_script();
172 script
.push_pointer(this);
173 script::slot saved
= script
.registry().push_field();
182 void regex::pattern(const string
& pattern
)
184 script
& script
= regex_script();
185 script
.push_pointer(this);
186 script
.push(pattern
);
187 script
.registry().set_field();
191 void regex::match(const string
& source
)
193 script
& script
= regex_script();
194 script
.push_pointer(this);
196 script
.globals().push_field("gmatch");
198 script
.push_pointer(this);
199 script
.registry().push_field();
202 script
.globals().set_field();
205 bool regex::get(string
& match
)
207 script
& script
= regex_script();
208 script
.push_pointer(this);
209 script::slot value
= script
.globals().push_field();
210 if (!value
.is_function())
212 script
.clear_stack();
217 bool result
= value
.get(match
);
222 bool regex::get(std::vector
<string
>& captures
)
224 script
& script
= regex_script();
225 script
.push_pointer(this);
226 script::slot value
= script
.globals().push_field();
227 if (!value
.is_function())
229 script
.clear_stack();
236 while (value
.is_string())
238 captures
.resize(captures
.size() + 1);
239 value
.get(captures
.back());
243 script
.clear_stack();
244 return 0 < captures
.size();
248 bool regex::match(string
& match
,
249 const string
& pattern
,
250 const string
& source
,
253 script
& script
= regex_script();
255 script::slot value
= script
.globals().push_field("match");
257 script
.push(pattern
);
258 ++position
; // Lua indices count from one.
259 script
.push(position
);
262 bool result
= value
.get(match
);
263 script
.clear_stack();
267 bool regex::match(std::vector
<string
>& captures
,
268 const string
& pattern
,
269 const string
& source
,
272 script
& script
= regex_script();
274 script::slot value
= script
.globals().push_field("match");
276 script
.push(pattern
);
277 ++position
; // Lua indices count from one.
278 script
.push(position
);
283 while (value
.is_string())
285 captures
.resize(captures
.size() + 1);
286 value
.get(captures
.back());
290 script
.clear_stack();
291 return 0 < captures
.size();
295 int regex::sub(string
& substitution
,
296 const string
& pattern
,
297 const string
& source
,
298 const string
& replacement
)
300 script
& script
= regex_script();
302 script::slot value
= script
.globals().push_field("gsub");
304 script
.push(pattern
);
305 script
.push(replacement
);
308 value
.get(substitution
);
314 script
.clear_stack();
This page took 0.045229 seconds and 3 git commands to generate.