blob: 69ea9ce9522b13e59c5c80a9394fce1f2fe02944 [file] [log] [blame]
Philip Liarddbb360c2011-02-25 09:02:23 +00001// Copyright (C) 2011 Google Inc.
2//
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>
22
23namespace i18n {
24namespace phonenumbers {
25
26using std::string;
27
28// Support string("hello") + 10
29string operator+(const string& s, int n);
30
31// Convert integer into string
32string SimpleItoa(int n);
33
34// Return true if 'in' starts with 'prefix' and write into 'out'
35// 'in' minus 'prefix'
36bool TryStripPrefixString(const string& in, const string& prefix, string* out);
37
38// Return true if 's' ends with 'suffix'
39bool HasSuffixString(const string& s, const string& suffix);
40
41
42// Hold a reference to a std::string or C string.
43class StringHolder {
44public:
45 // Don't make the constructors explicit to make the StrCat usage convenient.
46 StringHolder(const string& s);
47 StringHolder(const char* s);
48 ~StringHolder();
49
50 const string* GetString() const {
51 return string_;
52 }
53
54 const char* GetCString() const {
55 return cstring_;
56 }
57
58 size_t Length() const {
59 return len_;
60 }
61
62private:
63 const string* const string_;
64 const char* const cstring_;
65 const size_t len_;
66};
67
68
69string& operator+=(string& lhs, const StringHolder& rhs);
70
71
72// Efficient string concatenation
73
74string StrCat(const StringHolder& s1, const StringHolder& s2);
75
76string StrCat(const StringHolder& s1, const StringHolder& s2,
77 const StringHolder& s3);
78
79string StrCat(const StringHolder& s1, const StringHolder& s2,
80 const StringHolder& s3, const StringHolder& s4);
81
82string StrCat(const StringHolder& s1, const StringHolder& s2,
83 const StringHolder& s3, const StringHolder& s4,
84 const StringHolder& s5);
85
86string StrCat(const StringHolder& s1, const StringHolder& s2,
87 const StringHolder& s3, const StringHolder& s4,
88 const StringHolder& s5, const StringHolder& s6);
89
90string StrCat(const StringHolder& s1, const StringHolder& s2,
91 const StringHolder& s3, const StringHolder& s4,
92 const StringHolder& s5, const StringHolder& s6,
93 const StringHolder& s7, const StringHolder& s8,
94 const StringHolder& s9, const StringHolder& s10,
95 const StringHolder& s11);
96
97} // namespace phonenumbers
98} // namespace i18n
99
100#endif // I18N_PHONENUMBERS_STRINGUTIL_H_