Graphite  Version 3
An experimental 3D geometry processing program
string.h File Reference

Functions for string manipulation. More...

#include <geogram/basic/common.h>
#include <geogram/basic/numeric.h>
#include <string>
#include <sstream>
#include <stdexcept>
#include <iomanip>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <limits.h>

Go to the source code of this file.

Classes

class  GEO::String::ConversionError
 Conversion exception. More...
 

Namespaces

 GEO
 Global Vorpaline namespace.
 

Functions

void GEO::String::split_string (const std::string &in, char separator, std::vector< std::string > &out, bool skip_empty_fields=true)
 Splits a string into parts. More...
 
void GEO::String::split_string (const std::string &in, const std::string &separator, std::vector< std::string > &out, bool skip_empty_fields=true)
 Splits a string into parts. More...
 
bool GEO::String::split_string (const std::string &in, char separator, std::string &left, std::string &right)
 Splits a string into two parts. More...
 
std::string GEO::String::join_strings (const std::vector< std::string > &in, char separator)
 Join multiple strings. More...
 
std::string GEO::String::join_strings (const std::vector< std::string > &in, const std::string &separator)
 Join multiple strings. More...
 
std::string GEO::String::to_lowercase (const std::string &s)
 Converts a string to lowercase. More...
 
std::string GEO::String::to_uppercase (const std::string &s)
 Converts a string to uppercase. More...
 
std::string GEO::String::char_to_string (char c)
 Creates a one char string. More...
 
std::string GEO::String::quote (const std::string &s, char quotes='\"' )
 Adds quotes to a string. More...
 
bool GEO::String::string_starts_with (const std::string &haystack, const std::string &needle)
 Checks if a string starts with a substring. More...
 
bool GEO::String::string_ends_with (const std::string &haystack, const std::string &needle)
 Checks if a string ends with a substring. More...
 
std::string GEO::String::format (const char *format,...)
 Creates a string from a format string and additional arguments. Works like sprintf() More...
 
template<class T >
std::string GEO::String::to_string (const T &value)
 Converts a typed value to a string. More...
 
template<class T >
std::string GEO::String::to_display_string (const T &value)
 Converts a typed value to a string for display. More...
 
template<>
std::string GEO::String::to_display_string (const double &value)
 Converts a typed value to a string for display. More...
 
template<>
std::string GEO::String::to_display_string (const float &value)
 Converts a typed value to a string for display. More...
 
template<>
std::string GEO::String::to_string (const bool &value)
 Converts a boolean value to a string. More...
 
template<class T >
bool GEO::String::from_string (const char *s, T &value)
 Converts a C string to a typed value. More...
 
template<class T >
bool GEO::String::from_string (const std::string &s, T &value)
 Converts a std::string to a typed value. More...
 
template<>
bool GEO::String::from_string (const char *s, double &value)
 Converts a string to a double value. More...
 
template<typename T >
bool GEO::String::string_to_signed_integer (const char *s, T &value)
 Converts a string to a signed integer value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int8 &value)
 Converts a string to a Numeric::int8 value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int16 &value)
 Converts a string to a Numeric::int16 value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int32 &value)
 Converts a string to a Numeric::int32 value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int64 &value)
 Converts a string to a Numeric::int64 value.
 
template<typename T >
bool GEO::String::string_to_unsigned_integer (const char *s, T &value)
 Converts a string to a unsigned integer value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint8 &value)
 Converts a string to a Numeric::uint8 value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint16 &value)
 Converts a string to a Numeric::uint16 value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint32 &value)
 Converts a string to a Numeric::uint32 value. More...
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint64 &value)
 Converts a string to a Numeric::uint64 value.
 
template<>
bool GEO::String::from_string (const char *s, bool &value)
 Converts a string to a boolean value. More...
 
int GEO::String::to_int (const std::string &s)
 Converts a string to an int. More...
 
unsigned int GEO::String::to_uint (const std::string &s)
 Converts a string to an unsigned int. More...
 
double GEO::String::to_double (const std::string &s)
 Converts a string to a double. More...
 
bool GEO::String::to_bool (const std::string &s)
 Converts a string to a boolean. More...
 
std::string GEO::String::wchar_to_UTF8 (const wchar_t *in)
 Converts a wide char string into an UTF8 string. More...
 

Detailed Description

Functions for string manipulation.

Definition in file string.h.

Function Documentation

◆ char_to_string()

std::string GEO::String::char_to_string ( char  c)
inline

Creates a one char string.

Parameters
[in]cthe character to convert to a string
Returns
a string that contains characater c

Definition at line 175 of file string.h.

◆ format()

std::string GEO::String::format ( const char *  format,
  ... 
)

Creates a string from a format string and additional arguments. Works like sprintf()

Parameters
[in]formatthe format string

◆ from_string() [1/10]

template<>
bool GEO::String::from_string ( const char *  s,
bool &  value 
)
inline

Converts a string to a boolean value.

Legal values for the true boolean value are "true","True" and "1". Legal values for the false boolean value are "false","False" and "0".

Parameters
[in]sthe source string
[out]valuethe boolean value
Return values
trueif the conversion was successful
falseotherwise

Definition at line 517 of file string.h.

◆ from_string() [2/10]

template<>
bool GEO::String::from_string ( const char *  s,
double &  value 
)
inline

Converts a string to a double value.

Parameters
[in]sthe source string
[out]valuethe double value
Return values
trueif the conversion was successful
falseotherwise

Definition at line 360 of file string.h.

◆ from_string() [3/10]

template<>
bool GEO::String::from_string ( const char *  s,
Numeric::int16 value 
)
inline

Converts a string to a Numeric::int16 value.

See also
string_to_signed_integer()

Definition at line 409 of file string.h.

◆ from_string() [4/10]

template<>
bool GEO::String::from_string ( const char *  s,
Numeric::int32 value 
)
inline

Converts a string to a Numeric::int32 value.

See also
string_to_signed_integer()

Definition at line 418 of file string.h.

◆ from_string() [5/10]

template<>
bool GEO::String::from_string ( const char *  s,
Numeric::int8 value 
)
inline

Converts a string to a Numeric::int8 value.

See also
string_to_signed_integer()

Definition at line 400 of file string.h.

◆ from_string() [6/10]

template<>
bool GEO::String::from_string ( const char *  s,
Numeric::uint16 value 
)
inline

Converts a string to a Numeric::uint16 value.

See also
string_to_unsigned_integer()

Definition at line 478 of file string.h.

◆ from_string() [7/10]

template<>
bool GEO::String::from_string ( const char *  s,
Numeric::uint32 value 
)
inline

Converts a string to a Numeric::uint32 value.

See also
string_to_unsigned_integer()

Definition at line 487 of file string.h.

◆ from_string() [8/10]

template<>
bool GEO::String::from_string ( const char *  s,
Numeric::uint8 value 
)
inline

Converts a string to a Numeric::uint8 value.

See also
string_to_unsigned_integer()

Definition at line 469 of file string.h.

◆ from_string() [9/10]

template<class T >
bool GEO::String::from_string ( const char *  s,
T &  value 
)
inline

Converts a C string to a typed value.

This is a generic version that uses a std::istringstream to extract the value from the string. This function is specialized for integral types to reach the maximum efficiency.

Parameters
[in]sthe source string
[out]valuethe typed value
Return values
trueif the conversion was successful
falseotherwise

Definition at line 332 of file string.h.

◆ from_string() [10/10]

template<class T >
bool GEO::String::from_string ( const std::string &  s,
T &  value 
)
inline

Converts a std::string to a typed value.

This is a generic version that uses a std::istringstream to extract the value from the string. This function is specialized for integral types to reach the maximum efficiency.

Parameters
[in]sthe source string
[out]valuethe typed value
Return values
trueif the conversion was successful
falseotherwise

Definition at line 348 of file string.h.

◆ join_strings() [1/2]

std::string GEO::String::join_strings ( const std::vector< std::string > &  in,
char  separator 
)

Join multiple strings.

Joins all the strings in list in into a single string with each element separated by the given separator character.

Parameters
[in]inthe list of strings to join
[in]separatorthe separator character
Returns
the joined string
See also
split_string()

◆ join_strings() [2/2]

std::string GEO::String::join_strings ( const std::vector< std::string > &  in,
const std::string &  separator 
)

Join multiple strings.

Joins all the strings in list in into a single string with each element separated by the given separator string.

Parameters
[in]inthe list of strings to join
[in]separatorthe separator string (can be an empty string)
Returns
the joined string
See also
split_string()

◆ quote()

std::string GEO::String::quote ( const std::string &  s,
char  quotes = '\"' 
)

Adds quotes to a string.

Adds character quote at the beginning and the end of string s and returns the resulting string.

Parameters
[in]sthe string to quote
[in]quotesthe quoting char (default is '"')
Returns
the quoted string

◆ split_string() [1/3]

bool GEO::String::split_string ( const std::string &  in,
char  separator,
std::string &  left,
std::string &  right 
)

Splits a string into two parts.

Parameters
[in]inthe input string to split
[in]separatorthe separator character
[in]leftthe part of the input string on the left of the separator or the empty string if the separator did not appear in the input string
[in]rightthe right of the input string on the left of the separator or the empty string if the separator did not appear in the input string
Return values
trueif the separator was found in the input string
falseotherwise

◆ split_string() [2/3]

void GEO::String::split_string ( const std::string &  in,
char  separator,
std::vector< std::string > &  out,
bool  skip_empty_fields = true 
)

Splits a string into parts.

Splits the string in into a list of substrings out wherever separator occurs.

Parameters
[in]inthe input string to split
[in]separatorthe separator character
[in]outthe resulting list of substrings
[in]skip_empty_fieldsspecifies whether empty parts should be ignored and not stored in list out (this is true by default).
See also
join_strings()

◆ split_string() [3/3]

void GEO::String::split_string ( const std::string &  in,
const std::string &  separator,
std::vector< std::string > &  out,
bool  skip_empty_fields = true 
)

Splits a string into parts.

Splits the string in into a list of substrings out wherever separator occurs.

Parameters
[in]inthe input string to split
[in]separatorthe separator string
[in]outthe resulting list of substrings
[in]skip_empty_fieldsspecifies whether empty parts should be ignored and not stored in list out (this is true by default).
See also
join_strings()

◆ string_ends_with()

bool GEO::String::string_ends_with ( const std::string &  haystack,
const std::string &  needle 
)

Checks if a string ends with a substring.

Parameters
[in]haystackthe input string
[in]needlethe substring to check
Returns
true if haystack ends with needle, false otherwise.

◆ string_starts_with()

bool GEO::String::string_starts_with ( const std::string &  haystack,
const std::string &  needle 
)

Checks if a string starts with a substring.

Parameters
[in]haystackthe input string
[in]needlethe substring to check
Returns
true if haystack starts with needle, false otherwise.

◆ string_to_signed_integer()

template<typename T >
bool GEO::String::string_to_signed_integer ( const char *  s,
T &  value 
)
inline

Converts a string to a signed integer value.

Parameters
[in]sthe source string
[out]valuethe integer value
Return values
trueif the conversion was successful
falseotherwise

Definition at line 375 of file string.h.

◆ string_to_unsigned_integer()

template<typename T >
bool GEO::String::string_to_unsigned_integer ( const char *  s,
T &  value 
)
inline

Converts a string to a unsigned integer value.

Parameters
[in]sthe source string
[out]valuethe integer value
Return values
trueif the conversion was successful
falseotherwise

Definition at line 445 of file string.h.

◆ to_bool()

bool GEO::String::to_bool ( const std::string &  s)
inline

Converts a string to a boolean.

If the entire string cannot be converted to a boolean, the function throws an exception ConversionError.

Parameters
[in]sthe source string
Returns
the extracted boolean value
See also
ConversionError

Definition at line 595 of file string.h.

◆ to_display_string() [1/3]

template<>
std::string GEO::String::to_display_string ( const double &  value)
inline

Converts a typed value to a string for display.

Does not keep all significant digits for floating point numbers.

Parameters
[in]valuethe typed value to convert
Returns
a string that contain the stringified form of the value

Definition at line 269 of file string.h.

◆ to_display_string() [2/3]

template<>
std::string GEO::String::to_display_string ( const float &  value)
inline

Converts a typed value to a string for display.

Does not keep all significant digits for floating point numbers.

Parameters
[in]valuethe typed value to convert
Returns
a string that contain the stringified form of the value

Definition at line 283 of file string.h.

◆ to_display_string() [3/3]

template<class T >
std::string GEO::String::to_display_string ( const T &  value)
inline

Converts a typed value to a string for display.

Does not keep all significant digits for floating point numbers.

Parameters
[in]valuethe typed value to convert
Returns
a string that contain the stringified form of the value

Definition at line 256 of file string.h.

◆ to_double()

double GEO::String::to_double ( const std::string &  s)
inline

Converts a string to a double.

If the entire string cannot be converted to a double, the function throws an exception ConversionError.

Parameters
[in]sthe source string
Returns
the extracted double value
See also
ConversionError

Definition at line 578 of file string.h.

◆ to_int()

int GEO::String::to_int ( const std::string &  s)
inline

Converts a string to an int.

If the entire string cannot be converted to an int, the function throws an exception ConversionError.

Parameters
[in]sthe source string
Returns
the extracted integer value
See also
ConversionError

Definition at line 544 of file string.h.

◆ to_lowercase()

std::string GEO::String::to_lowercase ( const std::string &  s)

Converts a string to lowercase.

The conversion is done in place in the string s.

Parameters
[in,out]sThe string to convert
See also
to_uppercase()

◆ to_string() [1/2]

template<>
std::string GEO::String::to_string ( const bool &  value)
inline

Converts a boolean value to a string.

Parameters
[in]valuethe boolean value to convert
Returns
string "true" if the boolean value is true or "false" if the boolean value is false

Definition at line 296 of file string.h.

◆ to_string() [2/2]

template<class T >
std::string GEO::String::to_string ( const T &  value)
inline

Converts a typed value to a string.

Parameters
[in]valuethe typed value to convert
Returns
a string that contain the stringified form of the value

Definition at line 238 of file string.h.

◆ to_uint()

unsigned int GEO::String::to_uint ( const std::string &  s)
inline

Converts a string to an unsigned int.

If the entire string cannot be converted to an unsigned int, the function throws an exception ConversionError.

Parameters
[in]sthe source string
Returns
the extracted integer value
See also
ConversionError

Definition at line 561 of file string.h.

◆ to_uppercase()

std::string GEO::String::to_uppercase ( const std::string &  s)

Converts a string to uppercase.

The conversion is done in place in the string s.

Parameters
[in,out]sThe string to convert
See also
to_lowercase()

◆ wchar_to_UTF8()

std::string GEO::String::wchar_to_UTF8 ( const wchar_t *  in)

Converts a wide char string into an UTF8 string.

Parameters
[in]inthe input null-terminated wide-char string.
Returns
the UTF8-encoded string in a std::string.