blob: b0ea67b1f4bb76fb79ca4fd9e89f5ef6a4e0452d [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Hamaji776ca302015-06-06 03:52:48 +090015#ifndef STRUTIL_H_
16#define STRUTIL_H_
17
18#include <string>
19#include <vector>
20
21#include "string_piece.h"
22
23using namespace std;
24
25class 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 Hamajid87e59e2015-06-17 18:18:34 +090044 void Split(vector<StringPiece>* o);
45
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090046 private:
47 StringPiece in_;
48};
49
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090050class WordWriter {
51 public:
52 explicit WordWriter(string* o);
Shinichiro Hamaji37591ce2015-06-16 19:36:05 +090053 void MaybeAddWhitespace();
Shinichiro Hamaji2e6cbfc2015-06-16 18:46:50 +090054 void Write(StringPiece s);
55
56 private:
57 string* out_;
58 bool needs_space_;
59};
60
Shinichiro Hamaji8f68bd32015-06-18 11:01:51 +090061// Temporary modifies s[s.size()] to '\0'.
62class ScopedTerminator {
63 public:
64 explicit ScopedTerminator(StringPiece s);
65 ~ScopedTerminator();
66
67 private:
68 StringPiece s_;
69 char c_;
70};
71
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090072template <class String>
73inline 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 Hamaji02fc55b2015-06-16 17:19:07 +090084void AppendString(StringPiece str, string* out);
85
86bool HasPrefix(StringPiece str, StringPiece prefix);
87
88bool HasSuffix(StringPiece str, StringPiece suffix);
89
90StringPiece TrimSuffix(StringPiece str, StringPiece suffix);
91
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090092class Pattern {
93 public:
94 explicit Pattern(StringPiece pat);
Shinichiro Hamaji02fc55b2015-06-16 17:19:07 +090095
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090096 bool Match(StringPiece str) const;
Shinichiro Hamaji02fc55b2015-06-16 17:19:07 +090097
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090098 void AppendSubst(StringPiece str, StringPiece subst, string* out) const;
99
100 void AppendSubstRef(StringPiece str, StringPiece subst, string* out) const;
101
102 private:
103 bool MatchImpl(StringPiece str) const;
104
105 StringPiece pat_;
106 size_t percent_index_;
107};
Shinichiro Hamaji00cc6582015-06-17 18:12:46 +0900108
Shinichiro Hamaji810fd032015-06-17 04:38:03 +0900109string NoLineBreak(const string& s);
110
Shinichiro Hamaji32750622015-06-17 14:57:33 +0900111StringPiece TrimLeftSpace(StringPiece s);
112StringPiece TrimRightSpace(StringPiece s);
113StringPiece TrimSpace(StringPiece s);
114
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900115StringPiece Dirname(StringPiece s);
116StringPiece Basename(StringPiece s);
117StringPiece GetExt(StringPiece s);
118StringPiece StripExt(StringPiece s);
Shinichiro Hamaji5f57a992015-06-30 19:39:39 +0900119void NormalizePath(string* o, size_t start_index = 0);
Shinichiro Hamaji8a963582015-06-18 07:05:58 +0900120void AbsPath(StringPiece s, string* o);
Shinichiro Hamaji67f9a702015-06-18 06:00:57 +0900121
Shinichiro Hamajieafd0522015-06-18 16:46:02 +0900122size_t FindOutsideParen(StringPiece s, char c);
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +0900123size_t FindTwoOutsideParen(StringPiece s, char c1, char c2);
Shinichiro Hamajieafd0522015-06-18 16:46:02 +0900124
Shinichiro Hamaji14bb2792015-06-25 18:24:11 +0900125size_t FindEndOfLine(StringPiece s, size_t e, size_t* lf_cnt);
126
Shinichiro Hamajid9533322015-06-27 05:48:38 +0900127// Strip leading sequences of './' from file names, so that ./file
128// and file are considered to be the same file.
129// From http://www.gnu.org/software/make/manual/make.html#Features
130StringPiece TrimLeadingCurdir(StringPiece s);
131
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900132#endif // STRUTIL_H_