summaryrefslogtreecommitdiff
path: root/stringutil.cc
diff options
context:
space:
mode:
authorphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>2011-09-21 17:43:54 +0000
committerphilip.liard@gmail.com <philip.liard@gmail.com@ee073f10-1060-11df-b6a4-87a95322a99c>2011-09-21 17:43:54 +0000
commit6a0a07f4f0066eba2dc9bb81465f4e67d670c7b6 (patch)
tree0ae8d76ac1c43f0aada6fcf6b2c4449b8340e1da /stringutil.cc
parent70942011411fc60b15cddf0a3032b1614777dbc4 (diff)
downloadphonenumbers-6a0a07f4f0066eba2dc9bb81465f4e67d670c7b6.tar.gz
CPP: Add phonenumbermatcher.
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk/cpp/src/phonenumbers@356 ee073f10-1060-11df-b6a4-87a95322a99c
Diffstat (limited to 'stringutil.cc')
-rw-r--r--stringutil.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/stringutil.cc b/stringutil.cc
index e9d7a88..8d021a4 100644
--- a/stringutil.cc
+++ b/stringutil.cc
@@ -14,6 +14,7 @@
// Author: Philippe Liard
+#include <algorithm>
#include <cassert>
#include <cstring>
#include <sstream>
@@ -23,6 +24,7 @@
namespace i18n {
namespace phonenumbers {
+using std::equal;
using std::stringstream;
string operator+(const string& s, int n) {
@@ -54,6 +56,43 @@ string SimpleItoa(uint64 n) {
return GenericSimpleItoa(n);
}
+bool HasPrefixString(const string& s, const string& prefix) {
+ return s.size() >= prefix.size() &&
+ equal(s.begin(), s.begin() + prefix.size(), prefix.begin());
+}
+
+size_t FindNth(const string& s, char c, int n) {
+ size_t pos = string::npos;
+
+ for (int i = 0; i < n; ++i) {
+ pos = s.find_first_of(c, pos + 1);
+ if (pos == string::npos) {
+ break;
+ }
+ }
+ return pos;
+}
+
+void SplitStringUsing(const string& s, const string& delimiter,
+ vector<string>* result) {
+ assert(result);
+ size_t start_pos = 0;
+ size_t find_pos = string::npos;
+ if (delimiter.empty()) {
+ return;
+ }
+ while ((find_pos = s.find(delimiter, start_pos)) != string::npos) {
+ const string substring = s.substr(start_pos, find_pos - start_pos);
+ if (!substring.empty()) {
+ result->push_back(substring);
+ }
+ start_pos = find_pos + delimiter.length();
+ }
+ if (start_pos != s.length()) {
+ result->push_back(s.substr(start_pos));
+ }
+}
+
void StripString(string* s, const char* remove, char replacewith) {
const char* str_start = s->c_str();
const char* str = str_start;
@@ -255,6 +294,25 @@ string StrCat(const StringHolder& s1, const StringHolder& s2,
string StrCat(const StringHolder& s1, const StringHolder& s2,
const StringHolder& s3, const StringHolder& s4,
const StringHolder& s5, const StringHolder& s6,
+ const StringHolder& s7, const StringHolder& s8) {
+ string result;
+ result.reserve(s1.Length() + s2.Length() + s3.Length() + s4.Length() +
+ s5.Length() + s6.Length() + s7.Length() + s8.Length() + 1);
+ result += s1;
+ result += s2;
+ result += s3;
+ result += s4;
+ result += s5;
+ result += s6;
+ result += s7;
+ result += s8;
+
+ return result;
+}
+
+string StrCat(const StringHolder& s1, const StringHolder& s2,
+ const StringHolder& s3, const StringHolder& s4,
+ const StringHolder& s5, const StringHolder& s6,
const StringHolder& s7, const StringHolder& s8,
const StringHolder& s9) {
string result;