Geogram Version 1.9.9
A programming library of geometric algorithms
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_BASIC_STRING
41#define GEOGRAM_BASIC_STRING
42
45
46#include <string>
47#include <sstream>
48#include <stdexcept>
49#include <iomanip>
50
51#include <vector>
52#include <stdlib.h>
53#include <string.h>
54#include <errno.h>
55#include <stdio.h>
56#include <limits.h>
57
63namespace GEO {
64
65 /*
66 * \brief String manipulation utilities.
67 */
68 namespace String {
69
81 void GEOGRAM_API split_string(
82 const std::string& in,
83 char separator,
84 std::vector<std::string>& out,
85 bool skip_empty_fields = true
86 );
87
99 void GEOGRAM_API split_string(
100 const std::string& in,
101 const std::string& separator,
102 std::vector<std::string>& out,
103 bool skip_empty_fields = true
104 );
105
119 bool GEOGRAM_API split_string(
120 const std::string& in,
121 char separator,
122 std::string& left,
123 std::string& right
124 );
125
126
140 bool GEOGRAM_API split_string(
141 const std::string& in,
142 const std::string& separator,
143 std::string& left,
144 std::string& right
145 );
146
156 std::string GEOGRAM_API join_strings(
157 const std::vector<std::string>& in,
158 char separator
159 );
160
170 std::string GEOGRAM_API join_strings(
171 const std::vector<std::string>& in,
172 const std::string& separator
173 );
174
181 std::string GEOGRAM_API to_lowercase(const std::string& s);
182
189 std::string GEOGRAM_API to_uppercase(const std::string& s);
190
196 inline std::string char_to_string(char c) {
197 char s[2];
198 s[0] = c;
199 s[1] = '\0';
200 return std::string(s);
201 }
202
211 std::string GEOGRAM_API quote(
212 const std::string& s, char quotes = '\"'
213 );
214
222 bool GEOGRAM_API string_starts_with(
223 const std::string& haystack, const std::string& needle
224 );
225
233 bool GEOGRAM_API string_ends_with(
234 const std::string& haystack, const std::string& needle
235 );
236
244 GEO_NODISCARD inline std::string remove_prefix(
245 const std::string& s, const std::string& prefix
246 ) {
247 if(string_starts_with(s, prefix)) {
248 return s.substr(prefix.length());
249 }
250 return s;
251 }
252
260 GEO_NODISCARD inline std::string remove_suffix(
261 const std::string& s, const std::string& suffix
262 ) {
263 if(string_ends_with(s, suffix)) {
264 return s.substr(0, s.length() - suffix.length());
265 }
266 return s;
267 }
268
275 GEO_NODISCARD inline std::string trim_spaces(const std::string& s) {
276 size_t first = s.find_first_not_of(' ');
277 if (first == std::string::npos) {
278 return s;
279 }
280 size_t last = s.find_last_not_of(' ');
281 return s.substr(first, (last - first + 1));
282 }
283
289 std::string GEOGRAM_API format(const char* format, ...)
290#ifndef GOMGEN
291#ifdef GEO_COMPILER_GCC_FAMILY
292 // Tells the compiler that format is a printf-like format
293 // string, so that it can check that the arguments match
294 // the format string and bark at you if it is not the case.
295 __attribute__ ((__format__(printf, 1, 2)))
296#endif
297#endif
298 ;
299
306 std::string GEOGRAM_API format_time(double seconds, bool HMS_only=false);
307
313 template <class T>
314 inline std::string to_string(const T& value) {
315 std::ostringstream out;
316 // Makes sure that double-precision number are displayed
317 // with a sufficient number of digits. This is important
318 // to avoid losing precision when using ASCII files.
319 out << std::setprecision(17);
320 out << value;
321 return out.str();
322 }
323
331 template <class T>
332 inline std::string to_display_string(const T& value) {
333 return to_string(value);
334 }
335
336
344 template <>
345 inline std::string to_display_string(const double& value) {
346 std::ostringstream out;
347 out << value;
348 return out.str();
349 }
350
358 template <>
359 inline std::string to_display_string(const float& value) {
360 std::ostringstream out;
361 out << value;
362 return out.str();
363 }
364
371 template <>
372 inline std::string to_string(const bool& value) {
373 return value ? "true" : "false";
374 }
375
382 class GEOGRAM_API ConversionError : public std::logic_error {
383 public:
389 ConversionError(const std::string& s, const std::string& type);
390
394 const char* what() const GEO_NOEXCEPT override;
395 };
396
407 template <class T>
408 inline bool from_string(const char* s, T& value) {
409 std::istringstream in(s);
410 return (in >> value) && (in.eof() || ((in >> std::ws) && in.eof()));
411 }
412
423 template <class T>
424 inline bool from_string(const std::string& s, T& value) {
425 return from_string(s.c_str(), value);
426 }
427
435 template <>
436 inline bool from_string(const char* s, double& value) {
437 errno = 0;
438 char* end;
439 value = strtod(s, &end);
440 return end != s && *end == '\0' && errno == 0;
441 }
442
450 template <typename T>
451 inline bool string_to_signed_integer(const char* s, T& value) {
452 errno = 0;
453 char* end;
454#ifdef GEO_OS_WINDOWS
455 Numeric::int64 v = _strtoi64(s, &end, 10);
456#else
457 Numeric::int64 v = strtoll(s, &end, 10);
458#endif
459 if(
460 end != s && *end == '\0' && errno == 0 &&
461 v >= std::numeric_limits<T>::min() &&
462 v <= std::numeric_limits<T>::max()
463 ) {
464 value = static_cast<T>(v);
465 return true;
466 }
467
468 return false;
469 }
470
475 template <>
476 inline bool from_string(const char* s, Numeric::int8& value) {
477 return string_to_signed_integer(s, value);
478 }
479
484 template <>
485 inline bool from_string(const char* s, Numeric::int16& value) {
486 return string_to_signed_integer(s, value);
487 }
488
493 template <>
494 inline bool from_string(const char* s, Numeric::int32& value) {
495 return string_to_signed_integer(s, value);
496 }
497
501 template <>
502 inline bool from_string(const char* s, Numeric::int64& value) {
503 errno = 0;
504 char* end;
505#ifdef GEO_OS_WINDOWS
506 value = _strtoi64(s, &end, 10);
507#else
508 value = strtoll(s, &end, 10);
509#endif
510 return end != s && *end == '\0' && errno == 0;
511 }
512
520 template <typename T>
521 inline bool string_to_unsigned_integer(const char* s, T& value) {
522 errno = 0;
523 char* end;
524#ifdef GEO_OS_WINDOWS
525 Numeric::uint64 v = _strtoui64(s, &end, 10);
526#else
527 Numeric::uint64 v = strtoull(s, &end, 10);
528#endif
529 if(
530 end != s && *end == '\0' && errno == 0 &&
531 v <= std::numeric_limits<T>::max()
532 ) {
533 value = static_cast<T>(v);
534 return true;
535 }
536
537 return false;
538 }
539
544 template <>
545 inline bool from_string(const char* s, Numeric::uint8& value) {
546 return string_to_unsigned_integer(s, value);
547 }
548
553 template <>
554 inline bool from_string(const char* s, Numeric::uint16& value) {
555 return string_to_unsigned_integer(s, value);
556 }
557
562 template <>
563 inline bool from_string(const char* s, Numeric::uint32& value) {
564 return string_to_unsigned_integer(s, value);
565 }
566
570 template <>
571 inline bool from_string(const char* s, Numeric::uint64& value) {
572 errno = 0;
573 char* end;
574#ifdef GEO_OS_WINDOWS
575 value = _strtoui64(s, &end, 10);
576#else
577 value = strtoull(s, &end, 10);
578#endif
579 return end != s && *end == '\0' && errno == 0;
580 }
581
592 template <>
593 inline bool from_string(const char* s, bool& value) {
594 if(strcmp(s, "true") == 0 ||
595 strcmp(s, "True") == 0 ||
596 strcmp(s, "1") == 0
597 ) {
598 value = true;
599 return true;
600 }
601 if(strcmp(s, "false") == 0 ||
602 strcmp(s, "False") == 0 ||
603 strcmp(s, "0") == 0
604 ) {
605 value = false;
606 return true;
607 }
608 return false;
609 }
610
620 inline int to_int(const std::string& s) {
621 int value;
622 if(!from_string(s, value)) {
623 throw ConversionError(s, "integer");
624 }
625 return value;
626 }
627
637 inline unsigned int to_uint(const std::string& s) {
638 unsigned int value;
639 if(!from_string(s, value)) {
640 throw ConversionError(s, "integer");
641 }
642 return value;
643 }
644
654 inline double to_double(const std::string& s) {
655 double value;
656 if(!from_string(s, value)) {
657 throw ConversionError(s, "double");
658 }
659 return value;
660 }
661
671 inline bool to_bool(const std::string& s) {
672 bool value;
673 if(!from_string(s, value)) {
674 throw ConversionError(s, "boolean");
675 }
676 return value;
677 }
678
684 std::string GEOGRAM_API wchar_to_UTF8(const wchar_t* in);
685 }
686}
687
688#endif
Conversion exception.
Definition string.h:382
ConversionError(const std::string &s, const std::string &type)
Constructs a conversion exception.
const char * what() const GEO_NOEXCEPT override
Gets the string identifying the exception.
Common include file, providing basic definitions. Should be included before anything else by all head...
uint8_t uint8
Definition numeric.h:136
uint64_t uint64
Definition numeric.h:145
int8_t int8
Definition numeric.h:124
int32_t int32
Definition numeric.h:130
uint16_t uint16
Definition numeric.h:139
uint32_t uint32
Definition numeric.h:142
int16_t int16
Definition numeric.h:127
int64_t int64
Definition numeric.h:133
Global Vorpaline namespace.
Definition basic.h:55
Types and functions for numbers manipulation.
Functions for string manipulation.
std::string to_string(const T &value)
Converts a typed value to a string.
Definition string.h:314
bool string_to_unsigned_integer(const char *s, T &value)
Converts a string to a unsigned integer value.
Definition string.h:521
std::string wchar_to_UTF8(const wchar_t *in)
Converts a wide char string into an UTF8 string.
std::string to_uppercase(const std::string &s)
Converts a string to uppercase.
std::string char_to_string(char c)
Creates a one char string.
Definition string.h:196
bool from_string(const char *s, T &value)
Converts a C string to a typed value.
Definition string.h:408
void split_string(const std::string &in, char separator, std::vector< std::string > &out, bool skip_empty_fields=true)
Splits a string into parts.
int to_int(const std::string &s)
Converts a string to an int.
Definition string.h:620
bool string_starts_with(const std::string &haystack, const std::string &needle)
Checks if a string starts with a substring.
bool string_ends_with(const std::string &haystack, const std::string &needle)
Checks if a string ends with a substring.
std::string format(const char *format,...)
Creates a string from a format string and additional arguments. Works like sprintf()
std::string format_time(double seconds, bool HMS_only=false)
Converts a time in seconds into a human-readable string.
std::string join_strings(const std::vector< std::string > &in, char separator)
Join multiple strings.
std::string quote(const std::string &s, char quotes='\"' )
Adds quotes to a string.
unsigned int to_uint(const std::string &s)
Converts a string to an unsigned int.
Definition string.h:637
GEO_NODISCARD std::string remove_suffix(const std::string &s, const std::string &suffix)
Removes a suffix from a string.
Definition string.h:260
std::string to_lowercase(const std::string &s)
Converts a string to lowercase.
std::string to_display_string(const T &value)
Converts a typed value to a string for display.
Definition string.h:332
GEO_NODISCARD std::string trim_spaces(const std::string &s)
Removes the leading and trailing spaces from a string.
Definition string.h:275
bool to_bool(const std::string &s)
Converts a string to a boolean.
Definition string.h:671
bool string_to_signed_integer(const char *s, T &value)
Converts a string to a signed integer value.
Definition string.h:451
double to_double(const std::string &s)
Converts a string to a double.
Definition string.h:654
GEO_NODISCARD std::string remove_prefix(const std::string &s, const std::string &prefix)
Removes a prefix from a string.
Definition string.h:244