Graphite Version 3
An experimental 3D geometry processing program
Loading...
Searching...
No Matches
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

namespace  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.
 
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.
 
bool GEO::String::split_string (const std::string &in, char separator, std::string &left, std::string &right)
 Splits a string into two parts.
 
bool GEO::String::split_string (const std::string &in, const std::string &separator, std::string &left, std::string &right)
 Splits a string into two parts.
 
std::string GEO::String::join_strings (const std::vector< std::string > &in, char separator)
 Join multiple strings.
 
std::string GEO::String::join_strings (const std::vector< std::string > &in, const std::string &separator)
 Join multiple strings.
 
std::string GEO::String::to_lowercase (const std::string &s)
 Converts a string to lowercase.
 
std::string GEO::String::to_uppercase (const std::string &s)
 Converts a string to uppercase.
 
std::string GEO::String::char_to_string (char c)
 Creates a one char string.
 
std::string GEO::String::quote (const std::string &s, char quotes='\"' )
 Adds quotes to a string.
 
bool GEO::String::string_starts_with (const std::string &haystack, const std::string &needle)
 Checks if a string starts with a substring.
 
bool GEO::String::string_ends_with (const std::string &haystack, const std::string &needle)
 Checks if a string ends with a substring.
 
GEO_NODISCARD std::string GEO::String::remove_prefix (const std::string &s, const std::string &prefix)
 Removes a prefix from a string.
 
GEO_NODISCARD std::string GEO::String::remove_suffix (const std::string &s, const std::string &suffix)
 Removes a suffix from a string.
 
GEO_NODISCARD std::string GEO::String::trim_spaces (const std::string &s)
 Removes the leading and trailing spaces from a string.
 
std::string GEO::String::format (const char *format,...)
 Creates a string from a format string and additional arguments. Works like sprintf()
 
std::string GEO::String::format_time (double seconds, bool HMS_only=false)
 Converts a time in seconds into a human-readable string.
 
template<class T >
std::string GEO::String::to_string (const T &value)
 Converts a typed value to a string.
 
template<class T >
std::string GEO::String::to_display_string (const T &value)
 Converts a typed value to a string for display.
 
template<>
std::string GEO::String::to_display_string (const double &value)
 Converts a typed value to a string for display.
 
template<>
std::string GEO::String::to_display_string (const float &value)
 Converts a typed value to a string for display.
 
template<>
std::string GEO::String::to_string (const bool &value)
 Converts a boolean value to a string.
 
template<class T >
bool GEO::String::from_string (const char *s, T &value)
 Converts a C string to a typed value.
 
template<class T >
bool GEO::String::from_string (const std::string &s, T &value)
 Converts a std::string to a typed value.
 
template<>
bool GEO::String::from_string (const char *s, double &value)
 Converts a string to a double value.
 
template<typename T >
bool GEO::String::string_to_signed_integer (const char *s, T &value)
 Converts a string to a signed integer value.
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int8 &value)
 Converts a string to a Numeric::int8 value.
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int16 &value)
 Converts a string to a Numeric::int16 value.
 
template<>
bool GEO::String::from_string (const char *s, Numeric::int32 &value)
 Converts a string to a Numeric::int32 value.
 
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.
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint8 &value)
 Converts a string to a Numeric::uint8 value.
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint16 &value)
 Converts a string to a Numeric::uint16 value.
 
template<>
bool GEO::String::from_string (const char *s, Numeric::uint32 &value)
 Converts a string to a Numeric::uint32 value.
 
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.
 
int GEO::String::to_int (const std::string &s)
 Converts a string to an int.
 
unsigned int GEO::String::to_uint (const std::string &s)
 Converts a string to an unsigned int.
 
double GEO::String::to_double (const std::string &s)
 Converts a string to a double.
 
bool GEO::String::to_bool (const std::string &s)
 Converts a string to a boolean.
 
std::string GEO::String::wchar_to_UTF8 (const wchar_t *in)
 Converts a wide char string into an UTF8 string.
 

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 196 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

◆ format_time()

std::string GEO::String::format_time ( double  seconds,
bool  HMS_only = false 
)

Converts a time in seconds into a human-readable string.

Parameters
[in]HMS_onlyif set, always returns a hh:mm:ss string, else returns the time in seconds and a (hh:mm:ss) if time is greater or equal to one minute.

◆ from_string() [1/12]

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 593 of file string.h.

◆ from_string() [2/12]

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 436 of file string.h.

◆ from_string() [3/12]

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 485 of file string.h.

◆ from_string() [4/12]

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 494 of file string.h.

◆ from_string() [5/12]

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

Converts a string to a Numeric::int64 value.

Definition at line 502 of file string.h.

◆ from_string() [6/12]

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 476 of file string.h.

◆ from_string() [7/12]

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 554 of file string.h.

◆ from_string() [8/12]

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 563 of file string.h.

◆ from_string() [9/12]

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

Converts a string to a Numeric::uint64 value.

Definition at line 571 of file string.h.

◆ from_string() [10/12]

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 545 of file string.h.

◆ from_string() [11/12]

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 408 of file string.h.

◆ from_string() [12/12]

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 424 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

◆ remove_prefix()

GEO_NODISCARD std::string GEO::String::remove_prefix ( const std::string &  s,
const std::string &  prefix 
)
inline

Removes a prefix from a string.

Parameters
[in]sthe string
[in]prefixthe prefix to be removed
Returns
a new string with the prefix removed or s if s does not starts with prefix

Definition at line 244 of file string.h.

◆ remove_suffix()

GEO_NODISCARD std::string GEO::String::remove_suffix ( const std::string &  s,
const std::string &  suffix 
)
inline

Removes a suffix from a string.

Parameters
[in]sthe string
[in]suffixthe suffix
Returns
a new string with the suffix removed or s if s does not ends with suffix

Definition at line 260 of file string.h.

◆ split_string() [1/4]

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/4]

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/4]

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

Splits a string into two parts.

Parameters
[in]inthe input string to split
[in]separatorthe separator string
[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() [4/4]

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 451 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 521 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 671 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 345 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 359 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 332 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 654 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 620 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 372 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 314 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 637 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()

◆ trim_spaces()

GEO_NODISCARD std::string GEO::String::trim_spaces ( const std::string &  s)
inline

Removes the leading and trailing spaces from a string.

Parameters
[in]sa const reference to a string
Returns
the same string as s with leading and trailing spaces removed

Definition at line 275 of file string.h.

◆ 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.