Home | History | Annotate | Download | only in kati
      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 
     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 
     44   void Split(vector<StringPiece>* o);
     45 
     46  private:
     47   StringPiece in_;
     48 };
     49 
     50 class WordWriter {
     51  public:
     52   explicit WordWriter(string* o);
     53   void MaybeAddWhitespace();
     54   void Write(StringPiece s);
     55 
     56  private:
     57   string* out_;
     58   bool needs_space_;
     59 };
     60 
     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 
     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 
     84 void AppendString(StringPiece str, string* out);
     85 
     86 bool HasPrefix(StringPiece str, StringPiece prefix);
     87 
     88 bool HasSuffix(StringPiece str, StringPiece suffix);
     89 
     90 bool HasWord(StringPiece str, StringPiece w);
     91 
     92 StringPiece TrimPrefix(StringPiece str, StringPiece suffix);
     93 
     94 StringPiece TrimSuffix(StringPiece str, StringPiece suffix);
     95 
     96 class Pattern {
     97  public:
     98   explicit Pattern(StringPiece pat);
     99 
    100   bool Match(StringPiece str) const;
    101 
    102   StringPiece Stem(StringPiece str) const;
    103 
    104   void AppendSubst(StringPiece str, StringPiece subst, string* out) const;
    105 
    106   void AppendSubstRef(StringPiece str, StringPiece subst, string* out) const;
    107 
    108  private:
    109   bool MatchImpl(StringPiece str) const;
    110 
    111   StringPiece pat_;
    112   size_t percent_index_;
    113 };
    114 
    115 string NoLineBreak(const string& s);
    116 
    117 StringPiece TrimLeftSpace(StringPiece s);
    118 StringPiece TrimRightSpace(StringPiece s);
    119 StringPiece TrimSpace(StringPiece s);
    120 
    121 StringPiece Dirname(StringPiece s);
    122 StringPiece Basename(StringPiece s);
    123 StringPiece GetExt(StringPiece s);
    124 StringPiece StripExt(StringPiece s);
    125 void NormalizePath(string* o);
    126 void AbsPath(StringPiece s, string* o);
    127 
    128 size_t FindOutsideParen(StringPiece s, char c);
    129 size_t FindTwoOutsideParen(StringPiece s, char c1, char c2);
    130 size_t FindThreeOutsideParen(StringPiece s, char c1, char c2, char c3);
    131 
    132 size_t FindEndOfLine(StringPiece s, size_t e, size_t* lf_cnt);
    133 
    134 // Strip leading sequences of './' from file names, so that ./file
    135 // and file are considered to be the same file.
    136 // From http://www.gnu.org/software/make/manual/make.html#Features
    137 StringPiece TrimLeadingCurdir(StringPiece s);
    138 
    139 void FormatForCommandSubstitution(string* s);
    140 
    141 string SortWordsInString(StringPiece s);
    142 
    143 string ConcatDir(StringPiece b, StringPiece n);
    144 
    145 string EchoEscape(const string &str);
    146 
    147 void EscapeShell(string* s);
    148 
    149 #endif  // STRUTIL_H_
    150