cpp-common
string.hh
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 2; -*-
2 
3 #pragma once
4 
11 #include "nvsl/error.hh"
12 #include <string>
13 #include <vector>
14 
15 namespace nvsl {
17  inline auto split(const std::string &str, const std::string &delim,
18  size_t assert_length = UINT64_MAX) {
19  std::vector<std::string> result;
20  size_t prev = 0, pos = 0;
21 
22  do {
23  pos = str.find(delim, prev);
24 
25  if (pos == std::string::npos) pos = str.length();
26 
27  std::string token = str.substr(prev, pos - prev);
28 
29  if (!token.empty()) result.push_back(token);
30 
31  prev = pos + delim.length();
32  } while (pos < str.length() && prev < str.length());
33 
34  if (assert_length != UINT64_MAX) {
35  NVSL_ASSERT(result.size() == assert_length, "Not enough tokens");
36  }
37 
38  return result;
39  }
40 
42  inline auto zip(const std::vector<std::string> arr,
43  const std::string join_str) {
44  std::string result = "";
45 
46  size_t iter = 0;
47  for (const auto &tok : arr) {
48  result += tok;
49 
50  if (iter++ != arr.size() - 1) result += join_str;
51  }
52 
53  return result;
54  }
55 
57  inline auto is_suffix(const std::string &suffix, const std::string &str) {
58  auto mismatch =
59  std::mismatch(suffix.rbegin(), suffix.rend(), str.rbegin()).first;
60  return mismatch == suffix.rend();
61  }
62 
64  inline auto is_prefix(const std::string &prefix, const std::string &str) {
65  auto mismatch =
66  std::mismatch(prefix.begin(), prefix.end(), str.begin()).first;
67  return mismatch == prefix.end();
68  }
69 
70  inline const std::string S(const char *c) { return std::string(c); }
71 
72  inline std::string ltrim(const std::string &str) {
73  const auto start = str.find_first_not_of(" \t\n");
74 
75  if (start == std::string::npos) return str;
76 
77  return str.substr(start, str.size() - start);
78  }
79 
80  inline std::string rtrim(const std::string &str) {
81  const auto end = str.find_last_not_of(" \t\n");
82 
83  if (end == std::string::npos) return str;
84 
85  return str.substr(0, end + 1);
86  }
87 
88  inline std::string trim(const std::string &str) { return ltrim(rtrim(str)); }
89 } // namespace nvsl
nvsl::zip
auto zip(const std::vector< std::string > arr, const std::string join_str)
Concat all the elements of a string vector into a stingle string.
Definition: string.hh:42
error.hh
Brief description here.
nvsl::is_prefix
auto is_prefix(const std::string &prefix, const std::string &str)
Checks if a string is prefix of another string.
Definition: string.hh:64
nvsl::split
auto split(const std::string &str, const std::string &delim, size_t assert_length=UINT64_MAX)
Split string using a delimeter into a vector of strings.
Definition: string.hh:17
nvsl::is_suffix
auto is_suffix(const std::string &suffix, const std::string &str)
Checks if a string is suffix of another string.
Definition: string.hh:57
NVSL_ASSERT
#define NVSL_ASSERT(cond, msg)
Assert a condition w/ msg and generate backtrace on fail.
Definition: error.hh:73