Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 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 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 15 | #ifndef STRUTIL_H_ |
| 16 | #define STRUTIL_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "string_piece.h" |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | class WordScanner { |
| 26 | public: |
| 27 | struct Iterator { |
| 28 | Iterator& operator++(); |
| 29 | StringPiece operator*() const; |
| 30 | bool operator!=(const Iterator& r) const { |
| 31 | return in != r.in || s != r.s || i != r.i; |
| 32 | } |
| 33 | |
| 34 | const StringPiece* in; |
| 35 | int s; |
| 36 | int i; |
| 37 | }; |
| 38 | |
| 39 | explicit WordScanner(StringPiece in); |
| 40 | |
| 41 | Iterator begin() const; |
| 42 | Iterator end() const; |
| 43 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 44 | void Split(vector<StringPiece>* o); |
| 45 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 46 | private: |
| 47 | StringPiece in_; |
| 48 | }; |
| 49 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 50 | class WordWriter { |
| 51 | public: |
| 52 | explicit WordWriter(string* o); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 53 | void MaybeAddWhitespace(); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 54 | void Write(StringPiece s); |
| 55 | |
| 56 | private: |
| 57 | string* out_; |
| 58 | bool needs_space_; |
| 59 | }; |
| 60 | |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 61 | // Temporary modifies s[s.size()] to '\0'. |
| 62 | class ScopedTerminator { |
| 63 | public: |
| 64 | explicit ScopedTerminator(StringPiece s); |
| 65 | ~ScopedTerminator(); |
| 66 | |
| 67 | private: |
| 68 | StringPiece s_; |
| 69 | char c_; |
| 70 | }; |
| 71 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 72 | template <class String> |
| 73 | inline string JoinStrings(vector<String> v, const char* sep) { |
| 74 | string r; |
| 75 | for (StringPiece s : v) { |
| 76 | if (!r.empty()) { |
| 77 | r += sep; |
| 78 | } |
| 79 | r.append(s.begin(), s.end()); |
| 80 | } |
| 81 | return r; |
| 82 | } |
| 83 | |
Shinichiro Hamaji | 02fc55b | 2015-06-16 17:19:07 +0900 | [diff] [blame] | 84 | void AppendString(StringPiece str, string* out); |
| 85 | |
| 86 | bool HasPrefix(StringPiece str, StringPiece prefix); |
| 87 | |
| 88 | bool HasSuffix(StringPiece str, StringPiece suffix); |
| 89 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 90 | bool HasWord(StringPiece str, StringPiece w); |
| 91 | |
Shinichiro Hamaji | 02fc55b | 2015-06-16 17:19:07 +0900 | [diff] [blame] | 92 | StringPiece TrimSuffix(StringPiece str, StringPiece suffix); |
| 93 | |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 94 | class Pattern { |
| 95 | public: |
| 96 | explicit Pattern(StringPiece pat); |
Shinichiro Hamaji | 02fc55b | 2015-06-16 17:19:07 +0900 | [diff] [blame] | 97 | |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 98 | bool Match(StringPiece str) const; |
Shinichiro Hamaji | 02fc55b | 2015-06-16 17:19:07 +0900 | [diff] [blame] | 99 | |
Shinichiro Hamaji | a7984ad | 2015-09-11 16:33:16 +0900 | [diff] [blame] | 100 | StringPiece Stem(StringPiece str) const; |
| 101 | |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 102 | void AppendSubst(StringPiece str, StringPiece subst, string* out) const; |
| 103 | |
| 104 | void AppendSubstRef(StringPiece str, StringPiece subst, string* out) const; |
| 105 | |
| 106 | private: |
| 107 | bool MatchImpl(StringPiece str) const; |
| 108 | |
| 109 | StringPiece pat_; |
| 110 | size_t percent_index_; |
| 111 | }; |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 112 | |
Shinichiro Hamaji | 810fd03 | 2015-06-17 04:38:03 +0900 | [diff] [blame] | 113 | string NoLineBreak(const string& s); |
| 114 | |
Shinichiro Hamaji | 3275062 | 2015-06-17 14:57:33 +0900 | [diff] [blame] | 115 | StringPiece TrimLeftSpace(StringPiece s); |
| 116 | StringPiece TrimRightSpace(StringPiece s); |
| 117 | StringPiece TrimSpace(StringPiece s); |
| 118 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 119 | StringPiece Dirname(StringPiece s); |
| 120 | StringPiece Basename(StringPiece s); |
| 121 | StringPiece GetExt(StringPiece s); |
| 122 | StringPiece StripExt(StringPiece s); |
Shinichiro Hamaji | b3591ed | 2015-08-10 20:06:50 +0900 | [diff] [blame] | 123 | void NormalizePath(string* o); |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 124 | void AbsPath(StringPiece s, string* o); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 125 | |
Shinichiro Hamaji | eafd052 | 2015-06-18 16:46:02 +0900 | [diff] [blame] | 126 | size_t FindOutsideParen(StringPiece s, char c); |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 127 | size_t FindTwoOutsideParen(StringPiece s, char c1, char c2); |
Shinichiro Hamaji | 415a9b1 | 2015-07-05 14:33:02 +0900 | [diff] [blame] | 128 | size_t FindThreeOutsideParen(StringPiece s, char c1, char c2, char c3); |
Shinichiro Hamaji | eafd052 | 2015-06-18 16:46:02 +0900 | [diff] [blame] | 129 | |
Shinichiro Hamaji | 14bb279 | 2015-06-25 18:24:11 +0900 | [diff] [blame] | 130 | size_t FindEndOfLine(StringPiece s, size_t e, size_t* lf_cnt); |
| 131 | |
Shinichiro Hamaji | d953332 | 2015-06-27 05:48:38 +0900 | [diff] [blame] | 132 | // Strip leading sequences of './' from file names, so that ./file |
| 133 | // and file are considered to be the same file. |
| 134 | // From http://www.gnu.org/software/make/manual/make.html#Features |
| 135 | StringPiece TrimLeadingCurdir(StringPiece s); |
| 136 | |
Shinichiro Hamaji | c9b0aca | 2015-07-31 16:47:56 +0900 | [diff] [blame] | 137 | void FormatForCommandSubstitution(string* s); |
| 138 | |
| 139 | string SortWordsInString(StringPiece s); |
| 140 | |
Shinichiro Hamaji | 5a71a8b | 2015-08-06 19:23:18 +0900 | [diff] [blame] | 141 | string ConcatDir(StringPiece b, StringPiece n); |
| 142 | |
Dan Willemsen | e6f6858 | 2015-08-21 11:13:28 -0700 | [diff] [blame] | 143 | string EchoEscape(const string str); |
| 144 | |
Shinichiro Hamaji | 505d0d2 | 2016-02-29 20:07:39 +0900 | [diff] [blame] | 145 | void EscapeShell(string* s); |
| 146 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 147 | #endif // STRUTIL_H_ |