Geogram Version 1.8.5
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
135 std::string GEOGRAM_API join_strings(
136 const std::vector<std::string>& in,
137 char separator
138 );
139
149 std::string GEOGRAM_API join_strings(
150 const std::vector<std::string>& in,
151 const std::string& separator
152 );
153
160 std::string GEOGRAM_API to_lowercase(const std::string& s);
161
168 std::string GEOGRAM_API to_uppercase(const std::string& s);
169
175 inline std::string char_to_string(char c) {
176 char s[2];
177 s[0] = c;
178 s[1] = '\0';
179 return std::string(s);
180 }
181
190 std::string GEOGRAM_API quote(
191 const std::string& s, char quotes = '\"'
192 );
193
201 bool GEOGRAM_API string_starts_with(
202 const std::string& haystack, const std::string& needle
203 );
204
212 bool GEOGRAM_API string_ends_with(
213 const std::string& haystack, const std::string& needle
214 );
215
221 template <class T>
222 inline std::string to_string(const T& value) {
223 std::ostringstream out;
224 // Makes sure that double-precision number are displayed
225 // with a sufficient number of digits. This is important
226 // to avoid losing precision when using ASCII files.
227 out << std::setprecision(17);
228 out << value;
229 return out.str();
230 }
231
239 template <class T>
240 inline std::string to_display_string(const T& value) {
241 return to_string(value);
242 }
243
244
252 template <>
253 inline std::string to_display_string(const double& value) {
254 std::ostringstream out;
255 out << value;
256 return out.str();
257 }
258
266 template <>
267 inline std::string to_display_string(const float& value) {
268 std::ostringstream out;
269 out << value;
270 return out.str();
271 }
272
279 template <>
280 inline std::string to_string(const bool& value) {
281 return value ? "true" : "false";
282 }
283
290 class GEOGRAM_API ConversionError : public std::logic_error {
291 public:
297 ConversionError(const std::string& s, const std::string& type);
298
302 const char* what() const GEO_NOEXCEPT override;
303 };
304
315 template <class T>
316 inline bool from_string(const char* s, T& value) {
317 std::istringstream in(s);
318 return (in >> value) && (in.eof() || ((in >> std::ws) && in.eof()));
319 }
320
331 template <class T>
332 inline bool from_string(const std::string& s, T& value) {
333 return from_string(s.c_str(), value);
334 }
335
343 template <>
344 inline bool from_string(const char* s, double& value) {
345 errno = 0;
346 char* end;
347 value = strtod(s, &end);
348 return end != s && *end == '\0' && errno == 0;
349 }
350
358 template <typename T>
359 inline bool string_to_signed_integer(const char* s, T& value) {
360 errno = 0;
361 char* end;
362#ifdef GEO_OS_WINDOWS
363 Numeric::int64 v = _strtoi64(s, &end, 10);
364#else
365 Numeric::int64 v = strtoll(s, &end, 10);
366#endif
367 if(
368 end != s && *end == '\0' && errno == 0 &&
369 v >= std::numeric_limits<T>::min() &&
370 v <= std::numeric_limits<T>::max()
371 ) {
372 value = static_cast<T>(v);
373 return true;
374 }
375
376 return false;
377 }
378
383 template <>
384 inline bool from_string(const char* s, Numeric::int8& value) {
385 return string_to_signed_integer(s, value);
386 }
387
392 template <>
393 inline bool from_string(const char* s, Numeric::int16& value) {
394 return string_to_signed_integer(s, value);
395 }
396
401 template <>
402 inline bool from_string(const char* s, Numeric::int32& value) {
403 return string_to_signed_integer(s, value);
404 }
405
409 template <>
410 inline bool from_string(const char* s, Numeric::int64& value) {
411 errno = 0;
412 char* end;
413#ifdef GEO_OS_WINDOWS
414 value = _strtoi64(s, &end, 10);
415#else
416 value = strtoll(s, &end, 10);
417#endif
418 return end != s && *end == '\0' && errno == 0;
419 }
420
428 template <typename T>
429 inline bool string_to_unsigned_integer(const char* s, T& value) {
430 errno = 0;
431 char* end;
432#ifdef GEO_OS_WINDOWS
433 Numeric::uint64 v = _strtoui64(s, &end, 10);
434#else
435 Numeric::uint64 v = strtoull(s, &end, 10);
436#endif
437 if(
438 end != s && *end == '\0' && errno == 0 &&
439 v <= std::numeric_limits<T>::max()
440 ) {
441 value = static_cast<T>(v);
442 return true;
443 }
444
445 return false;
446 }
447
452 template <>
453 inline bool from_string(const char* s, Numeric::uint8& value) {
454 return string_to_unsigned_integer(s, value);
455 }
456
461 template <>
462 inline bool from_string(const char* s, Numeric::uint16& value) {
463 return string_to_unsigned_integer(s, value);
464 }
465
470 template <>
471 inline bool from_string(const char* s, Numeric::uint32& value) {
472 return string_to_unsigned_integer(s, value);
473 }
474
478 template <>
479 inline bool from_string(const char* s, Numeric::uint64& value) {
480 errno = 0;
481 char* end;
482#ifdef GEO_OS_WINDOWS
483 value = _strtoui64(s, &end, 10);
484#else
485 value = strtoull(s, &end, 10);
486#endif
487 return end != s && *end == '\0' && errno == 0;
488 }
489
500 template <>
501 inline bool from_string(const char* s, bool& value) {
502 if(strcmp(s, "true") == 0 ||
503 strcmp(s, "True") == 0 ||
504 strcmp(s, "1") == 0
505 ) {
506 value = true;
507 return true;
508 }
509 if(strcmp(s, "false") == 0 ||
510 strcmp(s, "False") == 0 ||
511 strcmp(s, "0") == 0
512 ) {
513 value = false;
514 return true;
515 }
516 return false;
517 }
518
528 inline int to_int(const std::string& s) {
529 int value;
530 if(!from_string(s, value)) {
531 throw ConversionError(s, "integer");
532 }
533 return value;
534 }
535
545 inline unsigned int to_uint(const std::string& s) {
546 unsigned int value;
547 if(!from_string(s, value)) {
548 throw ConversionError(s, "integer");
549 }
550 return value;
551 }
552
562 inline double to_double(const std::string& s) {
563 double value;
564 if(!from_string(s, value)) {
565 throw ConversionError(s, "double");
566 }
567 return value;
568 }
569
579 inline bool to_bool(const std::string& s) {
580 bool value;
581 if(!from_string(s, value)) {
582 throw ConversionError(s, "boolean");
583 }
584 return value;
585 }
586
592 std::string GEOGRAM_API wchar_to_UTF8(const wchar_t* in);
593 }
594}
595
596#endif
597
Common include file, providing basic definitions. Should be included before anything else by all head...
Conversion exception.
Definition string.h:290
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.
uint8_t uint8
Definition numeric.h:90
uint64_t uint64
Definition numeric.h:99
int8_t int8
Definition numeric.h:78
int32_t int32
Definition numeric.h:84
uint16_t uint16
Definition numeric.h:93
uint32_t uint32
Definition numeric.h:96
int16_t int16
Definition numeric.h:81
int64_t int64
Definition numeric.h:87
Global Vorpaline namespace.
Definition algorithm.h:64
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:222
bool string_to_unsigned_integer(const char *s, T &value)
Converts a string to a unsigned integer value.
Definition string.h:429
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:175
bool from_string(const char *s, T &value)
Converts a C string to a typed value.
Definition string.h:316
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:528
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 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:545
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:240
bool to_bool(const std::string &s)
Converts a string to a boolean.
Definition string.h:579
bool string_to_signed_integer(const char *s, T &value)
Converts a string to a signed integer value.
Definition string.h:359
double to_double(const std::string &s)
Converts a string to a double.
Definition string.h:562