blob: 04f131234174aadc06afc3933a0bade7167af791 [file] [log] [blame]
Philippe Liardb59d9082011-08-18 11:41:24 +00001// Copyright (C) 2011 The Libphonenumber Authors
Philip Liarddbb360c2011-02-25 09:02:23 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Author: Philippe Liard
16
17#ifndef I18N_PHONENUMBERS_STRINGUTIL_H_
18#define I18N_PHONENUMBERS_STRINGUTIL_H_
19
20#include <cstddef>
21#include <string>
Philippe Liard7b30af62011-09-21 17:43:54 +000022#include <vector>
Philip Liarddbb360c2011-02-25 09:02:23 +000023
Philippe Liard84ffc1d2013-04-30 11:35:55 +000024#include "phonenumbers/base/basictypes.h"
Philip Liardd4e1c732011-02-25 15:51:21 +000025
Philip Liarddbb360c2011-02-25 09:02:23 +000026namespace i18n {
27namespace phonenumbers {
28
29using std::string;
Philippe Liard7b30af62011-09-21 17:43:54 +000030using std::vector;
Philip Liarddbb360c2011-02-25 09:02:23 +000031
Philip Liard6f1ba2d2011-04-07 07:27:47 +000032// Supports string("hello") + 10.
Philippe Liard1b09e6d2012-05-23 09:20:57 +000033string operator+(const string& s, int n); // NOLINT(runtime/string)
Philip Liarddbb360c2011-02-25 09:02:23 +000034
Philip Liard6f1ba2d2011-04-07 07:27:47 +000035// Converts integer to string.
Philip Liardd4e1c732011-02-25 15:51:21 +000036string SimpleItoa(uint64 n);
Philippe Liard31b15982012-06-01 15:33:18 +000037string SimpleItoa(int64 n);
Philip Liarddbb360c2011-02-25 09:02:23 +000038string SimpleItoa(int n);
39
Philippe Liard7b30af62011-09-21 17:43:54 +000040// Returns whether the provided string starts with the supplied prefix.
41bool HasPrefixString(const string& s, const string& prefix);
42
43// Returns the index of the nth occurence of c in s or string::npos if less than
44// n occurrences are present.
45size_t FindNth(const string& s, char c, int n);
46
47// Splits a string using a character delimiter. Appends the components to the
48// provided vector. Note that empty tokens are ignored.
49void SplitStringUsing(const string& s, const string& delimiter,
50 vector<string>* result);
51
Philip Liard6f1ba2d2011-04-07 07:27:47 +000052// Replaces any occurrence of the character 'remove' (or the characters
53// in 'remove') with the character 'replacewith'.
54void StripString(string* s, const char* remove, char replacewith);
55
56// Returns true if 'in' starts with 'prefix' and writes 'in' minus 'prefix' into
57// 'out'.
Philip Liarddbb360c2011-02-25 09:02:23 +000058bool TryStripPrefixString(const string& in, const string& prefix, string* out);
59
Philip Liard6f1ba2d2011-04-07 07:27:47 +000060// Returns true if 's' ends with 'suffix'.
Philip Liarddbb360c2011-02-25 09:02:23 +000061bool HasSuffixString(const string& s, const string& suffix);
62
Philip Liard6f1ba2d2011-04-07 07:27:47 +000063// Converts string to int32.
64void safe_strto32(const string& s, int32 *n);
Philip Liarddbb360c2011-02-25 09:02:23 +000065
Philip Liard6f1ba2d2011-04-07 07:27:47 +000066// Converts string to uint64.
67void safe_strtou64(const string& s, uint64 *n);
68
Philippe Liard31b15982012-06-01 15:33:18 +000069// Converts string to int64.
70void safe_strto64(const string& s, int64* n);
71
Philip Liarde5786922011-04-26 09:04:58 +000072// Remove all occurrences of a given set of characters from a string.
73void strrmm(string* s, const string& chars);
74
Philip Liard791ec352011-05-16 16:12:25 +000075// Replaces all instances of 'substring' in 's' with 'replacement'. Returns the
76// number of instances replaced. Replacements are not subject to re-matching.
77int GlobalReplaceSubstring(const string& substring, const string& replacement,
78 string* s);
79
Philip Liard6f1ba2d2011-04-07 07:27:47 +000080// Holds a reference to a std::string or C string. It can also be constructed
81// from an integer which is converted to a string.
Philip Liarddbb360c2011-02-25 09:02:23 +000082class StringHolder {
Philippe Liard1b09e6d2012-05-23 09:20:57 +000083 public:
Philip Liarddbb360c2011-02-25 09:02:23 +000084 // Don't make the constructors explicit to make the StrCat usage convenient.
Philippe Liard1b09e6d2012-05-23 09:20:57 +000085 StringHolder(const string& s); // NOLINT(runtime/explicit)
86 StringHolder(const char* s); // NOLINT(runtime/explicit)
87 StringHolder(uint64 n); // NOLINT(runtime/explicit)
Philip Liarddbb360c2011-02-25 09:02:23 +000088 ~StringHolder();
89
90 const string* GetString() const {
91 return string_;
92 }
93
94 const char* GetCString() const {
95 return cstring_;
96 }
97
98 size_t Length() const {
99 return len_;
100 }
101
Philippe Liard1b09e6d2012-05-23 09:20:57 +0000102 private:
Philip Liard6f1ba2d2011-04-07 07:27:47 +0000103 const string converted_string_; // Stores the string converted from integer.
Philip Liarddbb360c2011-02-25 09:02:23 +0000104 const string* const string_;
105 const char* const cstring_;
106 const size_t len_;
107};
108
Philip Liarddbb360c2011-02-25 09:02:23 +0000109string& operator+=(string& lhs, const StringHolder& rhs);
110
Philip Liard6f1ba2d2011-04-07 07:27:47 +0000111// Efficient string concatenation.
Philip Liarddbb360c2011-02-25 09:02:23 +0000112
113string StrCat(const StringHolder& s1, const StringHolder& s2);
114
115string StrCat(const StringHolder& s1, const StringHolder& s2,
116 const StringHolder& s3);
117
118string StrCat(const StringHolder& s1, const StringHolder& s2,
119 const StringHolder& s3, const StringHolder& s4);
120
121string StrCat(const StringHolder& s1, const StringHolder& s2,
122 const StringHolder& s3, const StringHolder& s4,
123 const StringHolder& s5);
124
125string StrCat(const StringHolder& s1, const StringHolder& s2,
126 const StringHolder& s3, const StringHolder& s4,
127 const StringHolder& s5, const StringHolder& s6);
128
129string StrCat(const StringHolder& s1, const StringHolder& s2,
130 const StringHolder& s3, const StringHolder& s4,
131 const StringHolder& s5, const StringHolder& s6,
Philip Liard6f1ba2d2011-04-07 07:27:47 +0000132 const StringHolder& s7);
133
134string StrCat(const StringHolder& s1, const StringHolder& s2,
135 const StringHolder& s3, const StringHolder& s4,
136 const StringHolder& s5, const StringHolder& s6,
Philippe Liard7b30af62011-09-21 17:43:54 +0000137 const StringHolder& s7, const StringHolder& s8);
138
139string StrCat(const StringHolder& s1, const StringHolder& s2,
140 const StringHolder& s3, const StringHolder& s4,
141 const StringHolder& s5, const StringHolder& s6,
Philip Liarddbb360c2011-02-25 09:02:23 +0000142 const StringHolder& s7, const StringHolder& s8,
Philippe Liard4567abf2011-08-12 07:44:38 +0000143 const StringHolder& s9);
144
145string StrCat(const StringHolder& s1, const StringHolder& s2,
146 const StringHolder& s3, const StringHolder& s4,
147 const StringHolder& s5, const StringHolder& s6,
148 const StringHolder& s7, const StringHolder& s8,
Philip Liarddbb360c2011-02-25 09:02:23 +0000149 const StringHolder& s9, const StringHolder& s10,
150 const StringHolder& s11);
151
Lara Scheidegger7ae82ad2012-09-06 09:48:57 +0000152string StrCat(const StringHolder& s1, const StringHolder& s2,
153 const StringHolder& s3, const StringHolder& s4,
154 const StringHolder& s5, const StringHolder& s6,
155 const StringHolder& s7, const StringHolder& s8,
156 const StringHolder& s9, const StringHolder& s10,
157 const StringHolder& s11, const StringHolder& s12);
158
Philip Liard6f1ba2d2011-04-07 07:27:47 +0000159void StrAppend(string* dest, const StringHolder& s1);
160
161void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2);
162
Philippe Liarde55d3842012-03-15 17:04:03 +0000163void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
164 const StringHolder& s3);
165
166void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
167 const StringHolder& s3, const StringHolder& s4);
168
169void StrAppend(string* dest, const StringHolder& s1, const StringHolder& s2,
170 const StringHolder& s3, const StringHolder& s4,
171 const StringHolder& s5);
172
Philip Liarddbb360c2011-02-25 09:02:23 +0000173} // namespace phonenumbers
174} // namespace i18n
175
176#endif // I18N_PHONENUMBERS_STRINGUTIL_H_