libcyberradio  22.01.24
Pythonesque.cpp
1 /***************************************************************************
2  * \file Pythonesque.cpp
3  *
4  * \brief Provides string utility functions that mimic string methods from
5  * Python.
6  *
7  * \author DA
8  * \copyright Copyright (c) 2015-2021 CyberRadio Solutions, Inc.
9  *
10  */
11 
12 #include <LibCyberRadio/Common/Pythonesque.h>
13 #include <sstream>
14 
15 namespace LibCyberRadio
16 {
18  {
19  }
20 
22  {
23  }
24 
25  std::string Pythonesque::Lstrip(const std::string& str, const std::string& chars)
26  {
27  std::string ret;
28 
29  // Strip whitespace at beginning of the line
30  ret = str;
31  size_t found = ret.find_first_not_of(chars);
32  if ( found != std::string::npos ) {
33  ret = ret.substr(found);
34  }
35  else {
36  ret.clear();
37  }
38  return ret;
39  }
40 
41  std::string Pythonesque::Rstrip(const std::string& str, const std::string& chars)
42  {
43  std::string ret;
44 
45  // Strip whitespace at end of the line
46  ret = str;
47  size_t found = ret.find_last_not_of(chars);
48  if ( found != std::string::npos ) {
49  ret.erase(found+1);
50  }
51  else {
52  ret.clear();
53  }
54  return ret;
55  }
56 
57  std::string Pythonesque::Strip(const std::string& str, const std::string& chars)
58  {
59  return Rstrip(Lstrip(str, chars), chars);
60  }
61 
62  std::string Pythonesque::Replace(const std::string& str, const std::string &oldstr, const std::string& newstr, int count)
63  {
64  std::string buf(str);
65  std::string::size_type pos = buf.find(oldstr);
66  int replaces = 0;
67 
68  while ( (pos != std::string::npos) && (replaces < count) )
69  {
70  buf.replace(pos, oldstr.length(), newstr);
71  pos = buf.find(oldstr, pos + newstr.length());
72  replaces++;
73  }
74  return buf;
75  }
76 
77  BasicStringList Pythonesque::Split(const std::string& str, const std::string& sep, int maxsplit)
78  {
79  BasicStringList ret;
80  std::string buf(str);
81  std::string::size_type pos = buf.find(sep);
82  int splits = 0;
83  std::string tmp;
84 
85  while ( (pos != std::string::npos) && (splits < maxsplit) )
86  {
87  tmp = buf.substr(0, pos);
88  ret.push_back(tmp);
89  splits++;
90  buf = buf.substr(pos + sep.length());
91  pos = buf.find(sep);
92  }
93  if ( !buf.empty() )
94  ret.push_back(buf);
95  return ret;
96  }
97 
98  std::string Pythonesque::Join(const BasicStringList& vec, const std::string& sep)
99  {
100  std::ostringstream oss;
101  for (int i = 0; i < (int)vec.size(); i++)
102  {
103  oss << vec[i];
104  if ( i != (int)vec.size()-1 ) oss << sep;
105  }
106  return oss.str();
107  }
108 
109  bool Pythonesque::Startswith(const std::string& str, const std::string& prefix, int start, int end)
110  {
111  std::string buf = str.substr(start, end);
112  std::string::size_type pos = buf.find(prefix);
113  // Handle edge cases
114  if ( str.size() == 0 )
115  return false;
116  else if ( prefix.size() == 0 )
117  return false;
118  return ( pos == 0 );
119  }
120 
121  bool Pythonesque::Endswith(const std::string& str, const std::string& suffix, int start, int end)
122  {
123  std::string buf = str.substr(start, end);
124  std::string::size_type pos = buf.rfind(suffix);
125  // Handle edge cases
126  if ( str.size() == 0 )
127  return false;
128  else if ( suffix.size() == 0 )
129  return false;
130  return ( pos == (buf.length() - suffix.length()) );
131  }
132 
133  std::string Pythonesque::Basename(const std::string& path)
134  {
135  std::string ret = "";
136  std::string pathsep;
137  // Intelligently determine what the path separator is from the path,
138  // or use the OS to determine it
139  if ( path.find("\\") != std::string::npos )
140  pathsep = "\\";
141  else if ( path.find("/") != std::string::npos )
142  pathsep = "/";
143  else
144  {
145 #ifdef _WIN32
146  pathsep = "\\";
147 #else
148  pathsep = "/";
149 #endif
150  }
151  BasicStringList vec = Split(path, pathsep);
152  if ( vec.size() > 0 )
153  ret = vec[vec.size()-1];
154  return ret;
155  }
156 
157 } /* namespace CyberRadio */
static std::string Rstrip(const std::string &str, const std::string &chars=" \\\)
Strips trailing whitespace from the given string.
Definition: Pythonesque.cpp:41
static std::string Strip(const std::string &str, const std::string &chars=" \\\)
Strips both leading and trailing whitespace from the given string.
Definition: Pythonesque.cpp:57
virtual ~Pythonesque(void)
Destructor.
Definition: Pythonesque.cpp:21
static std::string Replace(const std::string &str, const std::string &oldstr, const std::string &newstr, int count=INT_MAX)
Replaces occurrences of one substring with another within the given string.
Definition: Pythonesque.cpp:62
static BasicStringList Split(const std::string &str, const std::string &sep, int maxsplit=INT_MAX)
Splits the given string into a list of string tokens.
Definition: Pythonesque.cpp:77
BASIC_LIST_CONTAINER< std::string > BasicStringList
Type representing a list of strings.
Definition: BasicList.h:25
Pythonesque(void)
Protected constructor; prevents class instantiation.
Definition: Pythonesque.cpp:17
static bool Startswith(const std::string &str, const std::string &prefix, int start=0, int end=INT_MAX)
Determines if the given string starts with the specified prefix.
static bool Endswith(const std::string &str, const std::string &suffix, int start=0, int end=INT_MAX)
Determines if the given string ends with the specified suffix.
Defines functionality for LibCyberRadio applications.
Definition: App.h:23
static std::string Lstrip(const std::string &str, const std::string &chars=" \\\)
Strips leading whitespace from the given string.
Definition: Pythonesque.cpp:25
static std::string Basename(const std::string &path)
Gets the base name (the file name itself, without leading path components) from the given file path...
static std::string Join(const BasicStringList &vec, const std::string &sep)
Joins a list of string tokens, concatenating them into a single string.
Definition: Pythonesque.cpp:98