Apache Singa
A General Distributed Deep Learning Library
string.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef SINGA_UTILS_TOKENIZER_H_
23 #define SINGA_UTILS_TOKENIZER_H_
24 
25 #include <string>
26 #include <algorithm>
27 #include "singa/utils/logging.h"
28 
29 namespace singa {
30 inline bool icasecmp(const string& l, const string& r) {
31  return l.size() == r.size() &&
32  equal(l.cbegin(), l.cend(), r.cbegin(),
33  [](string::value_type l1, string::value_type r1) {
34  return toupper(l1) == toupper(r1);
35  });
36 }
37 
38 inline string ToLowerCase(const string& input) {
39  string out;
40  out.resize(input.size());
41  std::transform(input.begin(), input.end(), out.begin(), ::tolower);
42  return out;
43 }
44 
45 inline int ArgPos(int argc, char** arglist, const char* arg) {
46  for (int i = 0; i < argc; i++) {
47  if (strcmp(arglist[i], arg) == 0) {
48  return i;
49  }
50  }
51  return -1;
52 }
53 
54 template<typename T>
55 inline std::string VecToStr(const std::vector<T> & in) {
56  std::string out = "(";
57 
58  for (auto x : in) {
59  out += std::to_string(x) + ", ";
60  }
61  out += ")";
62  return out;
63 }
64 
77 class Tokenizer {
78  public:
79  Tokenizer(const std::string& str, const std::string& sep): start_(0),
80  sep_(sep), buf_(str) {}
81  Tokenizer & operator>>(std::string& out) {
82  CHECK_LT(start_, buf_.length());
83  int start = start_;
84  auto pos = buf_.find_first_of(sep_, start);
85  if (pos == std::string::npos)
86  pos = buf_.length();
87  start_ = (unsigned int)(pos + 1);
88  out = buf_.substr(start, pos);
89  return *this;
90  }
91  bool Valid() { return start_ < buf_.length(); }
92 
93  private:
94  unsigned start_;
95  std::string sep_;
96  const std::string& buf_;
97 };
98 
99 } // namespace singa
100 
101 #endif // SINGA_UTILS_TOKENIZER_H_
Tokenize a string.
Definition: string.h:77
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48