OpenVDB  11.0.0
Name.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #ifndef OPENVDB_UTIL_NAME_HAS_BEEN_INCLUDED
5 #define OPENVDB_UTIL_NAME_HAS_BEEN_INCLUDED
6 
7 #include <openvdb/Platform.h>
8 #include <openvdb/version.h>
9 #include <string>
10 #include <iostream>
11 #include <vector>
12 #include <set>
13 #include <algorithm>
14 
15 namespace openvdb {
17 namespace OPENVDB_VERSION_NAME {
18 
19 typedef std::string Name;
20 
21 inline Name
22 readString(std::istream& is)
23 {
24  uint32_t size;
25  is.read(reinterpret_cast<char*>(&size), sizeof(uint32_t));
26  std::string buffer(size, ' ');
27  if (size>0) is.read(&buffer[0], size);
28  return buffer;
29 }
30 
31 
32 inline void
33 writeString(std::ostream& os, const Name& name)
34 {
35  uint32_t size = uint32_t(name.size());
36  os.write(reinterpret_cast<char*>(&size), sizeof(uint32_t));
37  os.write(&name[0], size);
38 }
39 
40 namespace string {
41 
42 template <typename ContainerT>
43 inline void split(ContainerT& out, const std::string& in, const char delim)
44 {
45  out.clear();
46  if (in.empty()) return;
47  std::string::size_type pos = 0u, old = 0u;
48  while ((pos = in.find(delim, pos)) != std::string::npos) {
49  out.insert(out.end(), in.substr(old, (pos-old)));
50  old = ++pos;
51  }
52  // last element
53  out.insert(out.end(), in.substr(old, in.length()-old));
54 }
55 
56 template <typename ContainerT>
57 inline void split(ContainerT& out, const std::string& in, const std::set<char>& delims)
58 {
59  out.clear();
60  if (in.empty()) return;
61  std::string::size_type pos = 0u, old = 0u;
62  const std::string seq(delims.begin(), delims.end());
63  while ((pos = in.find_first_of(seq, pos)) != std::string::npos) {
64  out.insert(out.end(), in.substr(old, (pos-old)));
65  old = ++pos;
66  }
67  // last element
68  out.insert(out.end(), in.substr(old, in.length()-old));
69 }
70 
71 inline bool starts_with(const std::string& in, const std::string& start)
72 {
73  if (start.length() > in.length()) return false;
74  return std::equal(start.begin(), start.end(), in.begin());
75 }
76 
77 inline bool ends_with(const std::string& in, const std::string& end)
78 {
79  if (end.length() > in.length()) return false;
80  return std::equal(end.rbegin(), end.rend(), in.rbegin());
81 }
82 
83 inline void trim(std::string& s)
84 {
85  // ltrim
86  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
87  return !std::isspace(ch);
88  }));
89 
90  // rtrim
91  s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
92  return !std::isspace(ch);
93  }).base(), s.end());
94 }
95 
96 inline void to_lower(std::string& s)
97 {
98  std::transform(s.begin(), s.end(), s.begin(),
99  [](auto c){ return std::tolower(c); });
100 }
101 
102 } // util
103 
104 } // namespace OPENVDB_VERSION_NAME
105 } // namespace openvdb
106 
107 #endif // OPENVDB_UTIL_NAME_HAS_BEEN_INCLUDED
Name readString(std::istream &is)
Definition: Name.h:22
void writeString(std::ostream &os, const Name &name)
Definition: Name.h:33
void to_lower(std::string &s)
Definition: Name.h:96
void split(ContainerT &out, const std::string &in, const std::set< char > &delims)
Definition: Name.h:57
bool ends_with(const std::string &in, const std::string &end)
Definition: Name.h:77
Definition: Exceptions.h:13
std::string Name
Definition: Name.h:19
void trim(std::string &s)
Definition: Name.h:83
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
bool starts_with(const std::string &in, const std::string &start)
Definition: Name.h:71
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212