OLD | NEW |
1 /* | 1 /* |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 void ReplaceString(std::wstring& input, const std::wstring& placeholder, const s
td::wstring& replacement); | 62 void ReplaceString(std::wstring& input, const std::wstring& placeholder, const s
td::wstring& replacement); |
63 | 63 |
64 /** | 64 /** |
65 * Returns the beginning of the URL which includes the scheme and hierarchical | 65 * Returns the beginning of the URL which includes the scheme and hierarchical |
66 * part according to http://en.wikipedia.org/wiki/URI_scheme. | 66 * part according to http://en.wikipedia.org/wiki/URI_scheme. |
67 */ | 67 */ |
68 std::wstring GetSchemeAndHierarchicalPart(const std::wstring& url); | 68 std::wstring GetSchemeAndHierarchicalPart(const std::wstring& url); |
69 | 69 |
70 std::wstring GetQueryString(const std::wstring& url); | 70 std::wstring GetQueryString(const std::wstring& url); |
71 | 71 |
| 72 /* |
| 73 * The method used below for trimming strings is taken from here: |
| 74 * http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstr
ing |
| 75 */ |
| 76 template<class C> |
| 77 bool isNotWhitespace(C x) |
| 78 { |
| 79 return !std::isspace(x, std::locale::classic()); |
| 80 }; |
| 81 |
| 82 template<class T> |
| 83 void TrimStringInPlaceLeft(T& text) |
| 84 { |
| 85 text.erase(text.begin(), std::find_if(text.begin(), text.end(), isNotWhitespac
e<T::value_type>)); |
| 86 } |
| 87 |
| 88 template<class T> |
| 89 void TrimStringInPlaceRight(T& text) |
| 90 { |
| 91 text.erase(std::find_if(text.rbegin(), text.rend(), isNotWhitespace<T::value_t
ype>).base(), text.end()); |
| 92 } |
| 93 |
| 94 template<class T> |
| 95 void TrimStringInPlace(T& text) |
| 96 { |
| 97 TrimStringInPlaceRight(text); |
| 98 TrimStringInPlaceLeft(text); |
| 99 } |
| 100 |
| 101 template<class T> |
| 102 T TrimStringLeft(T text) |
| 103 { |
| 104 TrimStringInPlaceLeft(text); |
| 105 return text; |
| 106 } |
| 107 |
| 108 template<class T> |
| 109 T TrimStringRight(T text) |
| 110 { |
| 111 TrimStringInPlaceRight(text); |
| 112 return text; |
| 113 } |
| 114 |
72 template<class T> | 115 template<class T> |
73 T TrimString(T text) | 116 T TrimString(T text) |
74 { | 117 { |
75 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st
dstring | 118 TrimStringInPlaceRight(text); |
76 T trimmed(text); | 119 TrimStringInPlaceLeft(text); |
77 std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_
type>, std::placeholders::_1, std::locale::classic()); | 120 return text; |
78 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st
d::not1(isspace))); | |
79 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(isspace
)).base(), trimmed.end()); | |
80 return trimmed; | |
81 } | 121 } |
82 | 122 |
83 #endif // UTILS_H | 123 #endif // UTILS_H |
OLD | NEW |