Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/Utils.cpp

Issue 10459038: Clean up and fix Utils::ToUTF8String() and Utils::ToUTF16String() (Closed)
Patch Set: Created May 14, 2013, 1:54 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/Utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <sstream> 18 #include <sstream>
19 #include <string> 19 #include <string>
20 20
21 #include "Utils.h" 21 #include "Utils.h"
22 #ifdef _WIN32 22 #ifdef _WIN32
23 #include <Windows.h> 23 #include <Windows.h>
24 #include <Shlwapi.h> 24 #include <Shlwapi.h>
25 25
26 #include <algorithm> 26 #include <algorithm>
27 #include <cctype> 27 #include <cctype>
28 #include <functional> 28 #include <functional>
29 #include <stdexcept>
29 30
30 #endif 31 #endif
31 32
32 using namespace AdblockPlus; 33 using namespace AdblockPlus;
33 34
34 std::string Utils::Slurp(std::ios& stream) 35 std::string Utils::Slurp(std::ios& stream)
35 { 36 {
36 std::stringstream content; 37 std::stringstream content;
37 content << stream.rdbuf(); 38 content << stream.rdbuf();
38 return content.str(); 39 return content.str();
39 } 40 }
40 41
41 std::string Utils::FromV8String(v8::Handle<v8::Value> value) 42 std::string Utils::FromV8String(v8::Handle<v8::Value> value)
42 { 43 {
43 v8::String::Utf8Value stringValue(value); 44 v8::String::Utf8Value stringValue(value);
44 if (stringValue.length()) 45 if (stringValue.length())
45 return std::string(*stringValue, stringValue.length()); 46 return std::string(*stringValue, stringValue.length());
46 else 47 else
47 return std::string(); 48 return std::string();
48 } 49 }
49 50
50 v8::Local<v8::String> Utils::ToV8String(const std::string& str) 51 v8::Local<v8::String> Utils::ToV8String(const std::string& str)
51 { 52 {
52 return v8::String::New(str.c_str(), str.length()); 53 return v8::String::New(str.c_str(), str.length());
53 } 54 }
54 55
55 #ifdef _WIN32 56 #ifdef _WIN32
56 std::wstring Utils::ToUTF16String(const std::string& str, unsigned long length) 57 std::wstring Utils::ToUTF16String(const std::string& str)
57 { 58 {
59 size_t length = str.size();
58 if (length == 0) 60 if (length == 0)
59 return std::wstring(); 61 return std::wstring();
60 DWORD utf16StringLength = 0; 62
61 std::wstring utf16String; 63 DWORD utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, NULL, 0);
62 utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &utf1 6String[0], utf16StringLength); 64 if (utf16StringLength == 0)
63 if (utf16StringLength > 0) 65 throw std::runtime_error("ToUTF16String failed. Can't determine the length o f the buffer needed.");
64 { 66
65 utf16String.resize(utf16StringLength); 67 std::wstring utf16String(utf16StringLength, L'\0');
66 utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &ut f16String[0], utf16StringLength); 68 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &utf16String[0], utf16Str ingLength);
67 return utf16String; 69 return utf16String;
68 }
69 std::runtime_error("ToUTF16String failed. Can't determine the length of the bu ffer needed\n");
70 return 0;
71 } 70 }
72 71
73 72 std::string Utils::ToUTF8String(const std::wstring& str)
74
75 std::string Utils::ToUTF8String(const std::wstring& str, unsigned long length)
76 { 73 {
74 size_t length = str.size();
77 if (length == 0) 75 if (length == 0)
78 return std::string(); 76 return std::string();
79 77
80 DWORD utf8StringLength = 0; 78 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, NULL, 0, 0, 0);
81 std::string utf8String; 79 if (utf8StringLength == 0)
82 utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8S tring[0], utf8StringLength, 0, 0); 80 throw std::runtime_error("ToUTF8String failed. Can't determine the length of the buffer needed.");
83 if (utf8StringLength > 0) 81
84 { 82 std::string utf8String(utf8StringLength, '\0');
85 utf8String.resize(utf8StringLength); 83 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Strin gLength, 0, 0);
86 utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf 8String[0], utf8StringLength, 0, 0); 84 return utf8String;
87 return utf8String;
88 }
89 std::runtime_error("ToUTF8String failed. Can't determine the length of the buf fer needed\n");
90 return 0;
91 } 85 }
92 86
93 std::wstring Utils::CanonizeUrl(std::wstring url) 87 std::wstring Utils::CanonizeUrl(std::wstring url)
94 { 88 {
95 HRESULT hr; 89 HRESULT hr;
96 90
97 std::wstring canonizedUrl; 91 std::wstring canonizedUrl;
98 DWORD canonizedUrlLength = 2049; // de-facto limit of url length 92 DWORD canonizedUrlLength = 2049; // de-facto limit of url length
99 93
100 canonizedUrl.resize(canonizedUrlLength); 94 canonizedUrl.resize(canonizedUrlLength);
(...skipping 15 matching lines...) Expand all
116 std::wstring Utils::TrimString(std::wstring text) 110 std::wstring Utils::TrimString(std::wstring text)
117 { 111 {
118 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 112 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
119 std::wstring trimmed(text); 113 std::wstring trimmed(text);
120 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace)))); 114 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace))));
121 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end()); 115 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
122 return trimmed; 116 return trimmed;
123 } 117 }
124 118
125 #endif 119 #endif
OLDNEW
« no previous file with comments | « src/Utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld