-/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+/*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
**] All rights reserved.
*
-* vi:ts=4 sw=4 tw=75
-*
* Distributable under the terms and conditions of the 2-clause BSD license;
* see the file COPYING for a complete text of the license.
*
-**************************************************************************/
+*****************************************************************************/
#include <stdexcept>
if (sizeof(wchar_t) == 2)
{
- size_t length = multi.length();
- buffer wide(new wchar_t[length + 1]);
- const UTF8* src1 = (const UTF8*)multi.c_str();
- const UTF8* src2 = src1 + length;
- UTF16* dst1 = (UTF16*)wide.get();
- UTF16* dst2 = dst1 + length+1;
+ size_t length = multi.length();
+ buffer wide(new wchar_t[length + 1]);
+ const UTF8* src1 = (const UTF8*)multi.c_str();
+ const UTF8* src2 = src1 + length;
+ UTF16* dst1 = (UTF16*)wide.get();
+ UTF16* dst2 = dst1 + length + 1;
if (ConvertUTF8toUTF16(&src1, src2,
- &dst1, dst2, lenientConversion) != conversionOK)
- {
+ &dst1, dst2,
+ lenientConversion) != conversionOK)
throw std::runtime_error("bad string conversion");
- }
*dst1 = 0;
wstring str(wide.get());
}
else if (sizeof(wchar_t) == 4)
{
- size_t length = multi.length();
- buffer wide(new wchar_t[length + 1]);
- const UTF8* src1 = (const UTF8*)multi.c_str();
- const UTF8* src2 = src1 + length;
- UTF32* dst1 = (UTF32*)wide.get();
- UTF32* dst2 = dst1 + length+1;
+ size_t length = multi.length();
+ buffer wide(new wchar_t[length + 1]);
+ const UTF8* src1 = (const UTF8*)multi.c_str();
+ const UTF8* src2 = src1 + length;
+ UTF32* dst1 = (UTF32*)wide.get();
+ UTF32* dst2 = dst1 + length + 1;
if (ConvertUTF8toUTF32(&src1, src2,
- &dst1, dst2, lenientConversion) != conversionOK)
- {
+ &dst1, dst2,
+ lenientConversion) != conversionOK)
throw std::runtime_error("bad string conversion");
- }
*dst1 = 0;
wstring str(wide.get());
if (sizeof(wchar_t) == 2)
{
- size_t length = wide.length();
- size_t multi_length = 3 * length + 1;
- buffer multi(new char[multi_length]);
+ size_t length = wide.length();
+ size_t multi_length = 3 * length + 1;
+ buffer multi(new char[multi_length]);
const UTF16* src1 = (const UTF16*)wide.c_str();
const UTF16* src2 = src1 + length;
- UTF8* dst1 = (UTF8*)multi.get();
- UTF8* dst2 = dst1 + multi_length;
+ UTF8* dst1 = (UTF8*)multi.get();
+ UTF8* dst2 = dst1 + multi_length;
if (ConvertUTF16toUTF8(&src1, src2,
- &dst1, dst2, lenientConversion) != conversionOK)
- {
+ &dst1, dst2,
+ lenientConversion) != conversionOK)
throw std::runtime_error("bad string conversion");
- }
*dst1 = 0;
string str(multi.get());
}
else if (sizeof(wchar_t) == 4)
{
- size_t length = wide.length();
- size_t multi_length = 4 * length + 1;
- buffer multi(new char[multi_length]);
+ size_t length = wide.length();
+ size_t multi_length = 4 * length + 1;
+ buffer multi(new char[multi_length]);
const UTF32* src1 = (const UTF32*)wide.c_str();
const UTF32* src2 = src1 + length;
- UTF8* dst1 = (UTF8*)multi.get();
- UTF8* dst2 = dst1 + multi_length;
+ UTF8* dst1 = (UTF8*)multi.get();
+ UTF8* dst2 = dst1 + multi_length;
if (ConvertUTF32toUTF8(&src1, src2,
- &dst1, dst2, lenientConversion) != conversionOK)
- {
+ &dst1, dst2,
+ lenientConversion) != conversionOK)
throw std::runtime_error("bad string conversion");
- }
*dst1 = 0;
string str(multi.get());
}
-static script& regex_script()
+static script& pattern_script()
{
static script script;
- static bool init = true;
+ static bool init = true;
if (init)
{
script.import_string_library();
return script;
}
-
-regex::regex(const string& pattern)
+pattern::pattern(const std::string& pattern)
{
- regex::pattern(pattern);
+ pattern::string(pattern);
}
-regex::regex(const string& pattern, const string& source)
+pattern::pattern(const std::string& pattern, const std::string& source)
{
- regex::pattern(pattern);
+ pattern::string(pattern);
match(source);
}
-regex::~regex()
+pattern::~pattern()
{
- script& script = regex_script();
-
+ script& script = pattern_script();
script.push_pointer(this);
script.push_nil();
script.globals().set_field();
script.registry().set_field();
}
-
-string regex::pattern() const
+std::string pattern::string() const
{
- script& script = regex_script();
+ script& script = pattern_script();
script.push_pointer(this);
script::slot saved = script.registry().push_field();
- string pattern;
+ std::string pattern;
saved.get(pattern);
saved.pop();
return pattern;
}
-void regex::pattern(const string& pattern)
+void pattern::string(const std::string& pattern)
{
- script& script = regex_script();
+ script& script = pattern_script();
script.push_pointer(this);
script.push(pattern);
script.registry().set_field();
}
-
-void regex::match(const string& source)
+void pattern::match(const std::string& source)
{
- script& script = regex_script();
+ script& script = pattern_script();
script.push_pointer(this);
script.globals().push_field("gmatch");
script.globals().set_field();
}
-bool regex::get(string& match)
+bool pattern::get(std::string& match)
{
- script& script = regex_script();
+ script& script = pattern_script();
script.push_pointer(this);
script::slot value = script.globals().push_field();
if (!value.is_function())
return result;
}
-bool regex::get(std::vector<string>& captures)
+bool pattern::get(std::vector<std::string>& captures)
{
- script& script = regex_script();
+ script& script = pattern_script();
script.push_pointer(this);
script::slot value = script.globals().push_field();
if (!value.is_function())
return 0 < captures.size();
}
-
-bool regex::match(string& match,
- const string& pattern,
- const string& source,
- int position)
+bool pattern::match(std::string& match,
+ const std::string& pattern, const std::string& source, int position)
{
- script& script = regex_script();
-
+ script& script = pattern_script();
script::slot value = script.globals().push_field("match");
script.push(source);
script.push(pattern);
- ++position; // Lua indices count from one.
+ ++position; // lua indices count from one
script.push(position);
script.call(3, 1);
return result;
}
-bool regex::match(std::vector<string>& captures,
- const string& pattern,
- const string& source,
- int position)
+bool pattern::match(std::vector<std::string>& captures,
+ const std::string& pattern, const std::string& source, int position)
{
- script& script = regex_script();
-
+ script& script = pattern_script();
script::slot value = script.globals().push_field("match");
script.push(source);
script.push(pattern);
- ++position; // Lua indices count from one.
+ ++position; // lua indices count from one
script.push(position);
script.call(3);
return 0 < captures.size();
}
-
-int regex::sub(string& substitution,
- const string& pattern,
- const string& source,
- const string& replacement)
+int pattern::sub(std::string& substitution,
+ const std::string& pattern,
+ const std::string& source, const std::string& replacement)
{
- script& script = regex_script();
-
+ script& script = pattern_script();
script::slot value = script.globals().push_field("gsub");
script.push(source);
script.push(pattern);