Thea
StringAlg.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2013
12 //
13 //============================================================================
14 
15 /*
16  ORIGINAL HEADER
17 
18  @file stringutils.h
19 
20  @maintainer Morgan McGuire, http://graphics.cs.williams.edu
21 
22  @author 2000-09-09
23  @edited 2010-03-05
24 */
25 
26 #ifndef __Thea_StringAlg_hpp_
27 #define __Thea_StringAlg_hpp_
28 
29 #include "Common.hpp"
30 #include "BasicStringAlg.hpp"
31 #include "Array.hpp"
32 #include <cctype>
33 #include <sstream>
34 #include <type_traits>
35 
36 #ifdef THEA_WINDOWS
37 # ifndef FNM_NOMATCH
38 # define FNM_NOMATCH 1 /* Match failed. */
39 # define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
40 # define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
41 # define FNM_PERIOD 0x04 /* Period must be matched by period. */
42 # define FNM_LEADING_DIR 0x08 /* Ignore /<tail> after Imatch. */
43 # define FNM_CASEFOLD 0x10 /* Case insensitive search. */
44 # endif
45 #else // On non-windows systems, include fnmatch directly. Both GNU/Linux and OS X support all the flags above.
46 # include <fnmatch.h>
47 #endif
48 
49 namespace Thea {
50 
54 template <typename T, typename std::enable_if< std::is_arithmetic<T>::value, T >::type * = nullptr>
55 T
56 fromString(std::string const & str)
57 {
58  size_t idx = 0;
59  T x = std::is_integral<T>::value ? (std::is_signed<T>::value ? static_cast<T>(std::stoll(str, &idx))
60  : static_cast<T>(std::stoull(str, &idx)))
61  : static_cast<T>(std::stold(str, &idx));
62 
63  // Check that the rest of the string is only whitespace
64  for (size_t i = idx; i < str.size(); ++i)
65  if (!std::isspace(str[i]))
66  throw Error("Could not convert string '" + str + "' to scalar");
67 
68  return x;
69 }
70 
81 THEA_API void parseCommaSeparated(std::string const & s, Array<std::string> & array, bool strip_quotes = true);
82 
93 THEA_API intx stringSplit(std::string const & s, char split_char, Array<std::string> & result,
94  bool skip_empty_fields = false);
95 
107 THEA_API intx stringSplit(std::string const & s, std::string const & split_chars, Array<std::string> & result,
108  bool skip_empty_fields = false);
109 
114 template <typename IteratorT, typename C>
115 std::string
116 stringJoin(IteratorT begin, IteratorT end, C const & padding)
117 {
118  std::ostringstream out;
119  for (IteratorT iter = begin; iter != end; ++iter)
120  {
121  if (iter != begin) out << padding;
122  out << *iter;
123  }
124 
125  return out.str();
126 }
127 
132 template <typename ContainerT, typename C>
133 std::string
134 stringJoin(ContainerT const & a, C const & padding)
135 {
136  return stringJoin(a.begin(), a.end(), padding);
137 }
138 
140 struct THEA_API Match
141 {
143  enum Value
144  {
146  NOESCAPE = FNM_NOESCAPE,
147 
152  PATHNAME = FNM_PATHNAME,
153 
158  PERIOD = FNM_PERIOD,
159 
165  LEADING_DIR = FNM_LEADING_DIR,
166 
168  CASEFOLD = FNM_CASEFOLD
169  };
170 
171  THEA_ENUM_CLASS_BODY(Match)
172 
173 }; // struct Match
174 
184 THEA_API bool patternMatch(std::string const & pattern, std::string const & query, int flags = 0);
185 
186 } // namespace Thea
187 
188 #endif
bool patternMatch(std::string const &pattern, std::string const &query, int flags)
Compares a filename or pathname to a pattern.
Definition: StringAlg.cpp:665
std::ptrdiff_t intx
A signed integer suitable for indexing a structure held in memory.
Definition: Platform.hpp:161
Root namespace for the Thea library.
void parseCommaSeparated(std::string const &s, Array< std::string > &array, bool strip_quotes)
Separates a comma-separated line, properly escaping commas within double quotes (") and super quotes ...
Definition: StringAlg.cpp:59
Value
Supported values.
Definition: StringAlg.hpp:143
T fromString(std::string const &str)
Convert a string to a scalar number.
Definition: StringAlg.hpp:56
intx stringSplit(std::string const &s, char split_char, Array< std::string > &result, bool skip_empty_fields)
Split a string at each occurrence of a splitting character.
Definition: StringAlg.cpp:246
std::string stringJoin(IteratorT begin, IteratorT end, C const &padding)
Concatenate a sequence of serializable objects (typically strings), separated by a padding character...
Definition: StringAlg.hpp:116
std::runtime_error Error
An error class.
Definition: Error.hpp:27
Pattern matching flags (enum class).
Definition: StringAlg.hpp:140
std::vector< T, Alloc > Array
Dynamically resizable array.
Definition: Array.hpp:25