Index: src/plugin/PluginUtil.cpp |
=================================================================== |
--- a/src/plugin/PluginUtil.cpp |
+++ b/src/plugin/PluginUtil.cpp |
@@ -2,26 +2,12 @@ |
#include <algorithm> |
#include <stdexcept> |
#include <vector> |
+#include <cctype> |
#include "../shared/Utils.h" |
#include "PluginUtil.h" |
#include "PluginSettings.h" |
-BString::BString(const std::wstring& value) |
- : value(::SysAllocString(value.c_str())) |
-{ |
-} |
- |
-BString::~BString() |
-{ |
- ::SysFreeString(value); |
-} |
- |
-BString::operator BSTR() |
-{ |
- return value; |
-} |
- |
std::wstring HtmlFolderPath() |
{ |
return GetDllDir() + L"html\\templates\\"; |
@@ -43,3 +29,138 @@ |
std::replace(url.begin(), url.end(), L'\\', L'/'); |
return L"file:///" + url; |
} |
+ |
+/** |
+ * Case-insensitive wide-string equality |
+ * |
+ * We only use any kind of case-insensitive comparison in 'InjectABP'. |
+ */ |
+bool ABP::util::wstring_equal_ci( std::wstring & a, std::wstring & b ) |
+{ |
Oleksandr
2014/06/26 00:48:43
Since these functions are generic enough, I think
Eric
2014/06/26 15:13:56
I'm OK with that.
I'd prefer to do it in a separa
|
+ /* |
+ * We don't need ordering, so first just compare lengths. |
+ */ |
+ size_t n = a.length(); |
+ if ( n != b.length() ) |
+ { |
+ return false; |
+ } |
+ // Assert 'a' and 'b' have the same length |
+ for ( size_t j = 0 ; j < n ; ++ j ) |
+ { |
+ if ( std::tolower( a[j] ) != std::tolower( b[j] ) ) |
sergei
2014/07/08 11:58:34
Strictly speaking it's not a correct implementatio
Eric
2014/07/08 17:46:37
That's right, it's not correct for every purpose.
|
+ { |
+ return false; |
+ } |
+ } |
+ return true; |
+} |
+ |
+void ABP::util::trim( std::wstring & s ) |
+{ |
Oleksandr
2014/06/26 00:48:43
We already gave AdblockPlus::Utils::TrimString
Eric
2014/06/26 15:13:56
I noticed that after I posted the review.
|
+ static wchar_t whitespace[] = L" \r\n\t"; |
+ size_t len = s.length(); |
+ if ( len == 0 ) |
+ { |
+ return; |
+ } |
+ size_t n = s.find_first_not_of( whitespace, 0, 4 ); |
+ // Assert substring s[0,n) is all whitespace |
+ // If n==0, the interval is empty. |
+ // If n==wstring::npos, the whole string matched |
+ if ( n == std::wstring::npos ) |
+ { |
+ s.clear(); |
+ return; |
+ } |
+ else if ( n > 0 ) |
+ { |
+ s.erase( 0, n ); |
+ len -= n; |
+ if ( len == 0 ) |
+ { |
+ return; |
+ } |
+ } |
+ // Assert 's' is not empty |
+ // Assert the first character of 's' is not whitespace |
+ // Assert len == s.length() |
+ n = s.find_last_not_of( whitespace, len, 4 ) + 1; |
+ if ( n < len ) |
+ { |
+ s.erase( n ); |
+ } |
+} |
+ |
+void ABP::util::trim_leading( std::wstring & s ) |
+{ |
+ static wchar_t whitespace[] = L" \r\n\t"; |
+ if ( s.length() == 0 ) |
+ { |
+ return; |
+ } |
+ size_t n = s.find_first_not_of( whitespace, 0, 4 ); |
+ // Assert substring s[0,n) is all whitespace |
+ // If n==0, the interval is empty, that is, no leading whitespace |
+ // If n==wstring::npos, the whole string matched, that is, it's all whitespace |
+ if ( n == std::wstring::npos ) |
+ { |
+ s.clear(); |
+ } |
+ else if ( n > 0 ) |
+ { |
+ s.erase( 0, n ); |
+ } |
+} |
+ |
+void ABP::util::trim_trailing( std::wstring & s ) |
+{ |
+ static wchar_t whitespace[] = L" \r\n\t"; |
+ size_t len = s.length(); |
+ if ( len == 0 ) |
+ { |
+ return; |
+ } |
+ // Assert 's' is not empty |
+ size_t n = s.find_last_not_of( whitespace, len, 4 ) + 1; |
+ if ( n < len ) |
+ { |
+ s.erase( n ); |
+ } |
+} |
+ |
+void ABP::util::to_lower( std::wstring & s ) |
+{ |
+ size_t n = s.length(); |
+ for ( size_t j = 0 ; j < n ; ++j ) |
+ { |
+ s[ j ] = tolower( s[ j ] ); |
+ } |
+} |
+ |
+std::wstring ABP::util::extract_token( const std::wstring source, size_t & start, const std::wstring tokens ) |
+{ |
+ // Skip initial tokens |
+ size_t begin = source.find_first_not_of( tokens, start ); |
+ if ( begin == std::wstring::npos ) |
+ { |
+ // Assert the source is all whitespace following the start position. |
+ start = begin; |
+ return std::wstring(); |
+ } |
+ // Assert n < source.length |
+ // Assert source[ start, n ) is all whitespace ( an empty interval if start == n ) |
+ // Assert source[ n ] is not whitespace |
+ size_t end = source.find_first_of( tokens, begin + 1 ); |
+ if ( end == std::wstring::npos ) |
+ { |
+ start = std::wstring::npos; |
+ return source.substr( begin ); |
+ } |
+ if ( end + 1 >= source.length() ) { |
+ start = std::wstring::npos; |
+ } else { |
+ start = end + 1; |
+ } |
+ return source.substr( begin, end - begin ); |
+} |