Thea
BasicStringAlg.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_BasicStringAlg_hpp_
27 #define __Thea_BasicStringAlg_hpp_
28 
29 #include "Platform.hpp"
30 #include <cctype>
31 #include <cstdarg>
32 #include <string>
33 
34 namespace Thea {
35 
37 extern THEA_API char const * NEWLINE;
38 
43 inline std::string
44 toString(char const * s)
45 {
46  return std::string(s ? s : "");
47 }
48 
56 inline intx
57 findFirstSlash(std::string const & f, intx start = 0)
58 {
59  size_t pos = f.find_first_of("/\\", (size_t)start);
60  return (pos == std::string::npos ? -1 : (intx)pos);
61 }
62 
71 inline intx
72 findLastSlash(std::string const & f, intx start = -1)
73 {
74  if (start == -1)
75  start = (intx)f.length() - 1;
76 
77  size_t pos = f.find_last_of("/\\", (size_t)start);
78  return (pos == std::string::npos ? -1 : (intx)pos);
79 }
80 
82 THEA_API bool beginsWith(std::string const & test, std::string const & pattern);
83 
85 THEA_API bool endsWith(std::string const & test, std::string const & pattern);
86 
92 THEA_API std::string wordWrap(std::string const & input, intx num_cols, char const * newline = NEWLINE);
93 
95 THEA_API std::string toUpper(std::string const & s);
96 
98 THEA_API std::string toLower(std::string const & s);
99 
101 THEA_API std::string trimWhitespace(std::string const & s);
102 
104 inline bool
106 {
107  return std::isspace(c) != 0;
108 }
109 
111 inline bool
112 isNewline(char c)
113 {
114  return (c == '\n') || (c == '\r');
115 }
116 
118 inline bool
119 isDigit(char c)
120 {
121  return std::isdigit(c) != 0;
122 }
123 
125 inline bool
126 isAlpha(char c)
127 {
128  return std::isalpha(c) != 0;
129 }
130 
132 inline bool
133 isSlash(char c)
134 {
135  return (c == '\\') || (c == '/');
136 }
137 
139 inline bool
140 isQuote(char c)
141 {
142  return (c == '\'') || (c == '\"');
143 }
144 
145 #ifdef __GNUC__
146 # define THEA_CHECK_PRINTF_ARGS __attribute__((__format__(__printf__, 1, 2)))
147 # define THEA_CHECK_VPRINTF_ARGS __attribute__((__format__(__printf__, 1, 0)))
148 # define THEA_CHECK_MEMBER_PRINTF_ARGS __attribute__((__format__(__printf__, 2, 3)))
149 # define THEA_CHECK_MEMBER_VPRINTF_ARGS __attribute__((__format__(__printf__, 2, 0)))
150 #else
151 # define THEA_CHECK_PRINTF_ARGS
152 # define THEA_CHECK_VPRINTF_ARGS
153 # define THEA_CHECK_MEMBER_PRINTF_ARGS
154 # define THEA_CHECK_MEMBER_VPRINTF_ARGS
155 #endif
156 
162 THEA_API std::string THEA_CDECL format(char const * fmt, ...) THEA_CHECK_PRINTF_ARGS;
163 
170 THEA_API std::string vformat(char const * fmt, va_list arg_ptr) THEA_CHECK_VPRINTF_ARGS;
171 
172 } // namespace Thea
173 
174 #endif
std::ptrdiff_t intx
A signed integer suitable for indexing a structure held in memory.
Definition: Platform.hpp:161
std::string toString(char const *s)
Convert a C-string to a std::string object, returning an empty string if the input is a null pointer...
bool beginsWith(std::string const &test, std::string const &pattern)
Check if the test string begins with the pattern string.
Definition: StringAlg.cpp:122
Root namespace for the Thea library.
bool isQuote(char c)
Check if a character is a quote (single or double).
bool isAlpha(char c)
Check if a character is a letter of the alphabet.
bool isWhitespace(char c)
Check if a character is a whitespace character.
bool endsWith(std::string const &test, std::string const &pattern)
Check if the test string ends with the pattern string.
Definition: StringAlg.cpp:143
std::string format(char const *fmt,...)
Produces a string from arguments in the style of printf.
Definition: StringAlg.cpp:305
std::string vformat(char const *fmt, va_list arg_ptr)
Produces a string from arguments in the style of printf, can be called with the argument list from a ...
Definition: StringAlg.cpp:414
std::string wordWrap(std::string const &input, intx num_cols, char const *newline=NEWLINE)
Produces a new string that is the input string wrapped at a certain number of columns (where the line...
Definition: StringAlg.cpp:167
intx findFirstSlash(std::string const &f, intx start=0)
Finds the index of the first &#39;\&#39; or &#39;/&#39; character, starting at index start.
std::string trimWhitespace(std::string const &s)
Strips whitespace from both ends of the string.
Definition: StringAlg.cpp:280
std::string toLower(std::string const &s)
Get the lowercase version of a string.
Definition: StringAlg.cpp:238
char const * NEWLINE
The newline character sequence for the current platform.
Definition: StringAlg.cpp:54
bool isNewline(char c)
Check if a character is a newline character.
std::string toUpper(std::string const &s)
Get the uppercase version of a string.
Definition: StringAlg.cpp:230
bool isSlash(char c)
Check if a character is a slash (forward or backward).
intx findLastSlash(std::string const &f, intx start=-1)
Finds the index of the last &#39;\&#39; or &#39;/&#39; character, starting at index start (starts at the end of the s...
bool isDigit(char c)
Check if a character is a digit.
This file must be included in every file in the project, before any other include.