X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fstring.cc;fp=src%2Fmoof%2Fstring.cc;h=a73bcd0723d0e9fd8f454fcc7c485513d0443686;hp=61bbf8fa6e77982a92b2c4fdf1542a24ab96a42b;hb=574af38ed616d1adfa5e6ce35f67cda1f707f89d;hpb=6c9943707d4f33035830eba0587a61a34eaecbc2 diff --git a/src/moof/string.cc b/src/moof/string.cc index 61bbf8f..a73bcd0 100644 --- a/src/moof/string.cc +++ b/src/moof/string.cc @@ -1,13 +1,11 @@ -/*] 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 @@ -27,18 +25,17 @@ wstring multi_to_wide(const string& multi) 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()); @@ -46,18 +43,17 @@ wstring multi_to_wide(const string& multi) } 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()); @@ -75,19 +71,18 @@ string wide_to_multi(const wstring& wide) 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()); @@ -95,19 +90,18 @@ string wide_to_multi(const wstring& wide) } 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()); @@ -120,10 +114,10 @@ string wide_to_multi(const wstring& wide) } -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(); @@ -141,22 +135,20 @@ static script& regex_script() 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(); @@ -165,32 +157,30 @@ regex::~regex() 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"); @@ -202,9 +192,9 @@ void regex::match(const string& source) 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()) @@ -219,9 +209,9 @@ bool regex::get(string& match) return result; } -bool regex::get(std::vector& captures) +bool pattern::get(std::vector& captures) { - script& script = regex_script(); + script& script = pattern_script(); script.push_pointer(this); script::slot value = script.globals().push_field(); if (!value.is_function()) @@ -244,18 +234,14 @@ bool regex::get(std::vector& captures) 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); @@ -264,17 +250,14 @@ bool regex::match(string& match, return result; } -bool regex::match(std::vector& captures, - const string& pattern, - const string& source, - int position) +bool pattern::match(std::vector& 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); @@ -291,14 +274,11 @@ bool regex::match(std::vector& captures, 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);