1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
/* bzflag
* Copyright (c) 1993-2025 Tim Riker
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the license found in the file
* named COPYING that should have accompanied this file.
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* common definitions
*/
#ifndef __TEXTUTILS_H__
#define __TEXTUTILS_H__
#include "common.h"
/* system interface headers */
#include <algorithm>
#include <ctype.h>
#ifdef HAVE_DEFINED_TOLOWER
#undef tolower
#undef toupper
#endif
#include <string>
#include <stdarg.h>
#include <vector>
/** This namespace provides basic functionality to parse and
* format strings
*/
namespace TextUtils
{
std::string vformat(const char* fmt, va_list args);
std::string format(const char* fmt, ...);
/** returns a string converted to lowercase
*/
inline std::string tolower(const std::string& s)
{
std::string trans = s;
for (std::string::iterator i=trans.begin(), end=trans.end(); i!=end; ++i)
*i = ::tolower(*i);
return trans;
}
/** returns a string converted to uppercase
*/
inline std::string toupper(const std::string& s)
{
std::string trans = s;
for (std::string::iterator i=trans.begin(), end=trans.end(); i!=end; ++i)
*i = ::toupper(*i);
return trans;
}
inline std::string ltrim(const std::string& s, const char* trim = " ")
{
if (!trim)
return s;
size_t pos = s.find_first_not_of(trim);
if (pos == std::string::npos)
return s;
return s.substr(pos, s.length() + 1);
}
inline std::string rtrim(const std::string& s, const char* trim = " ")
{
if (!trim)
return s;
size_t pos = s.find_last_not_of(trim);
if (pos == std::string::npos)
return s;
return s.substr(0, pos + 1);
}
inline std::string trim(const std::string& s, const char* trim = " ")
{
return (rtrim(ltrim(s, trim), trim));
}
/** replace all of in in replaceMe with withMe
*/
std::string replace_all(const std::string& in, const std::string& replaceMe, const std::string& withMe);
/** return copy of string with all whitespace stripped
*/
std::string no_whitespace(const std::string &s);
/**
* Get a vector of strings from a string, using all of chars of thedelims
* string as separators. If maxTokens > 0, then the last 'token' maycontain delimiters
* as it just returns the rest of the line
* if you specify use quotes then tokens can be grouped in quotes and delimeters
* inside quotes are ignored.
* Hence /ban "Mr Blahblah" isajerk parses to "/ban", "Mr Blahlah" and "isajerk"
* but so does "Mr Blahblah" "isajerk", so if you want 3 tokens and a delimeter
* is in one of the tokens, by putting quotes around it you can get the correct
* parsing. When use quotes is enabled, \'s and "'s should\can be escaped with \
* escaping is not currently done for any other character.
* Should not have " as a delimeter if you want to use quotes
*/
std::vector<std::string> tokenize(const std::string& in, const std::string &delims, const int maxTokens = 0,
const bool useQuotes = false);
/** convert a string representation of some duration into minutes
* example: "1d2h16m" -> 1500
* return true if the string can be parsed
*/
bool parseDuration(const char *duration, int &durationInt);
// C h a r a c t e r c o m p a r i s o n
/** compare_nocase is strait from Stroustrup. This implementation uses
* strings instead of char arrays and includes a maxlength bounds check.
* It compares two strings and returns 0 if equal, <0 if s1 is less than
* s2, and >0 if s1 is greater than s2.
*/
inline int compare_nocase(const std::string& s1, const std::string &s2, int maxlength=4096)
{
std::string::const_iterator p1 = s1.begin();
std::string::const_iterator p2 = s2.begin();
int i=0;
while (p1 != s1.end() && p2 != s2.end())
{
if (i >= maxlength)
return 0;
if (::tolower(*p1) != ::tolower(*p2))
return (::tolower(*p1) < ::tolower(*p2)) ? -1 : 1;
++p1;
++p2;
++i;
}
return (s2.size() == s1.size()) ? 0 : (s1.size() < s2.size()) ? -1 : 1; // size is unsigned
}
/** utility function returns truthfully whether
* given character is a letter.
*/
inline bool isAlphabetic(const char c)
{
if (( c > 64 && c < 91) ||
( c > 96 && c < 123))
return true;
return false;
}
/** utility function returns truthfully whether
* given character is a number.
*/
inline bool isNumeric(const char c)
{
if (( c > 47 && c < 58))
return true;
return false;
}
/** utility function returns truthfully whether
* a given character is printable whitespace.
* this includes newline, carriage returns, tabs
* and spaces.
*/
inline bool isWhitespace(const char c)
{
if ((( c >= 9 ) && ( c <= 13 )) ||
(c == 32))
return true;
return false;
}
/** utility function returns truthfully whether
* a given character is punctuation.
*/
inline bool isPunctuation(const char c)
{
if (( c > 32 && c < 48) ||
( c > 57 && c < 65) ||
( c > 90 && c < 97) ||
( c > 122 && c < 127))
return true;
return false;
}
/** utility function returns truthfully whether
* given character is an alphanumeric. this is
* strictly letters and numbers.
*/
inline bool isAlphanumeric(const char c)
{
if (isAlphabetic(c) || isNumeric(c))
return true;
return false;
}
/** utility function returns truthfully whether
* given character is printable. this includes
* letters, numbers, and punctuation.
* (but NOT whitespace)
*/
inline bool isVisible(const char c)
{
if (isAlphanumeric(c) || isPunctuation(c))
return true;
return false;
}
/** utility function returns truthfully whether
* given character is printable. this includes
* letters, numbers, punctuation, and whitespace
*/
inline bool isPrintable(const char c)
{
return (isVisible(c) || isWhitespace(c));
}
// S t r i n g i t e r a t i o n
/** utility method that returns the position of the
* first alphanumeric character from a string
*/
inline int firstAlphanumeric(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (isAlphanumeric(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first non-alphanumeric character from a string
*/
inline int firstNonalphanumeric(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (!isAlphanumeric(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first printable character from a string
*/
inline int firstPrintable(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (isPrintable(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first non-printable character from a string
*/
inline int firstNonprintable(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (!isPrintable(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first visible character from a string
*/
inline int firstVisible(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (isVisible(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first non visible character from a string (control
* codes or whitespace
*/
inline int firstNonvisible(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (!isVisible(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first alphabetic character from a string
*/
inline int firstAlphabetic(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (isAlphabetic(input[i]))
return i;
return -1;
}
/** utility method that returns the position of the
* first printable character from a string
*/
inline int firstNonalphabetic(const std::string &input, unsigned short int max=4096)
{
if (max > input.length())
max = (unsigned short)input.length();
for (unsigned short i = 0; i < max; i++)
if (!isAlphabetic(input[i]))
return i;
return -1;
}
/** integer to string
*/
inline std::string itoa(int value, const char* fmt = "%i")
{
char buf[64];
snprintf(buf, sizeof(buf), fmt, value);
return std::string(buf);
}
/** url-encodes a string
*/
std::string url_encode(const std::string &text);
/** url-decodes a string
*/
std::string url_decode(const std::string &text);
/** octal-encode nonprintable characters in a string
* the quotechar parameter will always be encoded; '"' is a sensible value
*/
std::string escape_nonprintable(const std::string &text, const char quotechar = '\\');
/** escape a string
*/
std::string escape(const std::string &text, char escaper);
/** un-escape a string
*/
std::string unescape(const std::string &text, char escaper);
/** lookup for an un-escaped separator
*/
int unescape_lookup(const std::string &text, char escaper, char sep);
/** return a copy of a string, truncated to specified length,
* make last char a '~' if truncation took place
*/
std::string str_trunc_continued (const std::string &text, int len);
/** find the first instance of a substring,
*/
size_t find_first_substr(const std::string &findin, const std::string &findwhat, size_t offset = 0);
}
#endif // __TEXTUTILS_H__
// Local Variables: ***
// mode: C++ ***
// tab-width: 4 ***
// c-basic-offset: 4 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=4 tabstop=4
|