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

Side by Side Diff: src/shared/Utils.h

Issue 5081266177179648: Issue 1104 - Cannot uncheck Disable on website option in tool bar (Closed)
Patch Set: Created Feb. 23, 2015, 12:40 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
OLDNEW
1 /* 1 /*
Eric 2015/02/26 16:14:10 We need unit tests for the new utility functions.
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 49
50 50
51 bool IsWindowsVistaOrLater(); 51 bool IsWindowsVistaOrLater();
52 bool IsWindows8OrLater(); 52 bool IsWindows8OrLater();
53 53
54 std::string ToUtf8String(const std::wstring& str); 54 std::string ToUtf8String(const std::wstring& str);
55 std::wstring ToUtf16String(const std::string& str); 55 std::wstring ToUtf16String(const std::string& str);
56 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& value); 56 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& value);
57 std::wstring GetDllDir(); 57 std::wstring GetDllDir();
58 std::wstring GetAppDataPath(); 58 std::wstring GetAppDataPath();
59 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement); 59
60 template<typename T>
61 void ReplaceString(T& input, const T& placeholder, const T& replacement)
Eric 2015/02/26 16:14:10 We want to be able to use type inference for this
62 {
63 auto replaceStart = input.find(placeholder);
64 if (replaceStart != T::npos)
65 {
66 input.replace(replaceStart, placeholder.length(), replacement);
67 }
68 }
69
70 template <typename T>
71 T ASCIIStringToLower(const T& text)
Eric 2015/02/26 16:14:10 We could take the opportunity to rename this "Asci
72 {
73 T textlower;
74 std::transform(text.begin(), text.end(), std::back_inserter(textlower),
75 [](T::value_type ch)
76 {
77 return std::tolower(ch, std::locale());
78 }
79 );
80 return textlower;
81 }
82
83 template<typename T>
84 bool BeginsWith(const T& value, const T& prefix)
85 {
86 return value.compare(0, prefix.length(), prefix) == 0;
87 }
Eric 2015/02/26 16:14:10 Here are the unit tests I had previously, adapted
Eric 2015/02/26 16:14:10 I wrote a version of this last summer in the giant
60 88
61 template<class T> 89 template<class T>
62 T TrimString(T text) 90 T TrimString(T text)
63 { 91 {
64 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 92 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
65 T trimmed(text); 93 T trimmed(text);
66 std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_ type>, std::placeholders::_1, std::locale::classic()); 94 std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_ type>, std::placeholders::_1, std::locale::classic());
67 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(isspace))); 95 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(isspace)));
68 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(isspace )).base(), trimmed.end()); 96 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(isspace )).base(), trimmed.end());
69 return trimmed; 97 return trimmed;
70 } 98 }
71 99
72 #endif // UTILS_H 100 #endif // UTILS_H
OLDNEW

Powered by Google App Engine
This is Rietveld