| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #include "PluginStdAfx.h" | 1 #include "PluginStdAfx.h" |
| 2 #include <algorithm> | 2 #include <algorithm> |
| 3 #include <stdexcept> | 3 #include <stdexcept> |
| 4 #include <vector> | 4 #include <vector> |
| 5 #include <cctype> | |
| 5 | 6 |
| 6 #include "../shared/Utils.h" | 7 #include "../shared/Utils.h" |
| 7 #include "PluginUtil.h" | 8 #include "PluginUtil.h" |
| 8 #include "PluginSettings.h" | 9 #include "PluginSettings.h" |
| 9 | 10 |
| 10 BString::BString(const std::wstring& value) | |
| 11 : value(::SysAllocString(value.c_str())) | |
| 12 { | |
| 13 } | |
| 14 | |
| 15 BString::~BString() | |
| 16 { | |
| 17 ::SysFreeString(value); | |
| 18 } | |
| 19 | |
| 20 BString::operator BSTR() | |
| 21 { | |
| 22 return value; | |
| 23 } | |
| 24 | |
| 25 std::wstring HtmlFolderPath() | 11 std::wstring HtmlFolderPath() |
| 26 { | 12 { |
| 27 return GetDllDir() + L"html\\templates\\"; | 13 return GetDllDir() + L"html\\templates\\"; |
| 28 } | 14 } |
| 29 | 15 |
| 30 std::wstring UserSettingsFileUrl() | 16 std::wstring UserSettingsFileUrl() |
| 31 { | 17 { |
| 32 return FileUrl(HtmlFolderPath() + L"index.html"); | 18 return FileUrl(HtmlFolderPath() + L"index.html"); |
| 33 } | 19 } |
| 34 | 20 |
| 35 std::wstring FirstRunPageFileUrl() | 21 std::wstring FirstRunPageFileUrl() |
| 36 { | 22 { |
| 37 return FileUrl(HtmlFolderPath() + L"firstRun.html"); | 23 return FileUrl(HtmlFolderPath() + L"firstRun.html"); |
| 38 } | 24 } |
| 39 | 25 |
| 40 std::wstring FileUrl(const std::wstring& path) | 26 std::wstring FileUrl(const std::wstring& path) |
| 41 { | 27 { |
| 42 std::wstring url = path; | 28 std::wstring url = path; |
| 43 std::replace(url.begin(), url.end(), L'\\', L'/'); | 29 std::replace(url.begin(), url.end(), L'\\', L'/'); |
| 44 return L"file:///" + url; | 30 return L"file:///" + url; |
| 45 } | 31 } |
| 32 | |
| 33 /** | |
| 34 * Case-insensitive wide-string equality | |
| 35 * | |
| 36 * We only use any kind of case-insensitive comparison in 'InjectABP'. | |
| 37 */ | |
| 38 bool ABP::util::wstring_equal_ci( std::wstring & a, std::wstring & b ) | |
| 39 { | |
|
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
| |
| 40 /* | |
| 41 * We don't need ordering, so first just compare lengths. | |
| 42 */ | |
| 43 size_t n = a.length(); | |
| 44 if ( n != b.length() ) | |
| 45 { | |
| 46 return false; | |
| 47 } | |
| 48 // Assert 'a' and 'b' have the same length | |
| 49 for ( size_t j = 0 ; j < n ; ++ j ) | |
| 50 { | |
| 51 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.
| |
| 52 { | |
| 53 return false; | |
| 54 } | |
| 55 } | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 void ABP::util::trim( std::wstring & s ) | |
| 60 { | |
|
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.
| |
| 61 static wchar_t whitespace[] = L" \r\n\t"; | |
| 62 size_t len = s.length(); | |
| 63 if ( len == 0 ) | |
| 64 { | |
| 65 return; | |
| 66 } | |
| 67 size_t n = s.find_first_not_of( whitespace, 0, 4 ); | |
| 68 // Assert substring s[0,n) is all whitespace | |
| 69 // If n==0, the interval is empty. | |
| 70 // If n==wstring::npos, the whole string matched | |
| 71 if ( n == std::wstring::npos ) | |
| 72 { | |
| 73 s.clear(); | |
| 74 return; | |
| 75 } | |
| 76 else if ( n > 0 ) | |
| 77 { | |
| 78 s.erase( 0, n ); | |
| 79 len -= n; | |
| 80 if ( len == 0 ) | |
| 81 { | |
| 82 return; | |
| 83 } | |
| 84 } | |
| 85 // Assert 's' is not empty | |
| 86 // Assert the first character of 's' is not whitespace | |
| 87 // Assert len == s.length() | |
| 88 n = s.find_last_not_of( whitespace, len, 4 ) + 1; | |
| 89 if ( n < len ) | |
| 90 { | |
| 91 s.erase( n ); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void ABP::util::trim_leading( std::wstring & s ) | |
| 96 { | |
| 97 static wchar_t whitespace[] = L" \r\n\t"; | |
| 98 if ( s.length() == 0 ) | |
| 99 { | |
| 100 return; | |
| 101 } | |
| 102 size_t n = s.find_first_not_of( whitespace, 0, 4 ); | |
| 103 // Assert substring s[0,n) is all whitespace | |
| 104 // If n==0, the interval is empty, that is, no leading whitespace | |
| 105 // If n==wstring::npos, the whole string matched, that is, it's all whitespace | |
| 106 if ( n == std::wstring::npos ) | |
| 107 { | |
| 108 s.clear(); | |
| 109 } | |
| 110 else if ( n > 0 ) | |
| 111 { | |
| 112 s.erase( 0, n ); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void ABP::util::trim_trailing( std::wstring & s ) | |
| 117 { | |
| 118 static wchar_t whitespace[] = L" \r\n\t"; | |
| 119 size_t len = s.length(); | |
| 120 if ( len == 0 ) | |
| 121 { | |
| 122 return; | |
| 123 } | |
| 124 // Assert 's' is not empty | |
| 125 size_t n = s.find_last_not_of( whitespace, len, 4 ) + 1; | |
| 126 if ( n < len ) | |
| 127 { | |
| 128 s.erase( n ); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void ABP::util::to_lower( std::wstring & s ) | |
| 133 { | |
| 134 size_t n = s.length(); | |
| 135 for ( size_t j = 0 ; j < n ; ++j ) | |
| 136 { | |
| 137 s[ j ] = tolower( s[ j ] ); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 std::wstring ABP::util::extract_token( const std::wstring source, size_t & start , const std::wstring tokens ) | |
| 142 { | |
| 143 // Skip initial tokens | |
| 144 size_t begin = source.find_first_not_of( tokens, start ); | |
| 145 if ( begin == std::wstring::npos ) | |
| 146 { | |
| 147 // Assert the source is all whitespace following the start position. | |
| 148 start = begin; | |
| 149 return std::wstring(); | |
| 150 } | |
| 151 // Assert n < source.length | |
| 152 // Assert source[ start, n ) is all whitespace ( an empty interval if start == n ) | |
| 153 // Assert source[ n ] is not whitespace | |
| 154 size_t end = source.find_first_of( tokens, begin + 1 ); | |
| 155 if ( end == std::wstring::npos ) | |
| 156 { | |
| 157 start = std::wstring::npos; | |
| 158 return source.substr( begin ); | |
| 159 } | |
| 160 if ( end + 1 >= source.length() ) { | |
| 161 start = std::wstring::npos; | |
| 162 } else { | |
| 163 start = end + 1; | |
| 164 } | |
| 165 return source.substr( begin, end - begin ); | |
| 166 } | |
| OLD | NEW |