1 #ifndef STK_UTIL_DIAG_StringUtil_h 2 #define STK_UTIL_DIAG_StringUtil_h 10 #include <stk_util/util/FeatureTest.hpp> 36 int case_strncmp(
const char *c1,
const char *c2,
size_t n);
52 inline int case_strcmp(
const std::string &s1,
const std::string &s2) {
74 const char *case_strstr(
const char *t,
const char *
find);
76 inline const char *case_strstr(
const std::string &s1,
const std::string &s2) {
77 return case_strstr(s1.c_str(), s2.c_str());
80 inline const char *case_strstr(
const String &s1,
const String &s2) {
81 return case_strstr(s1.c_str(), s2.c_str());
95 return s.empty() ?
"<undefined>" : s;
106 return std::toupper(c);
117 return std::tolower(c);
129 return std::isspace(c) ?
'_' : std::toupper(c);
141 return std::isspace(c) ?
'_' : std::tolower(c);
154 std::transform(name.begin(), name.end(), name.begin(),
to_upper);
169 std::transform(name.begin(), name.end(), name.begin(),
to_lower);
214 for (
char *c = name; *c != 0; ++c )
230 for (
char *c = name; *c != 0; ++c )
249 typename T::iterator it0 = name.begin();
250 while (it0 != name.end() && std::isspace(*it0))
253 typename T::iterator it1 = name.end();
254 while (it1 != it0 && std::isspace(*(it1 - 1)))
256 return name = T(it0, it1);
267 std::string
title(
const std::string &s);
270 return title(std::string(s.c_str()));
297 std::string
to_string(
const double &r,
int precision = 4);
309 std::string
to_string(
const float &r,
int precision = 4);
320 template <
typename T>
332 #ifdef SIERRA_USE_PLATFORM_DEMANGLER 334 std::string
demangle(
const char *symbol);
336 const char *
demangle(
const char *symbol);
349 std::string
format_time(
double t,
const char *format =
"%b %e %Y %H:%M:%S");
369 std::string
word_wrap(
const std::string &s,
unsigned int line_length,
370 const std::string &prefix,
const std::string &prefix_first_line);
386 inline std::string
word_wrap(
const std::string &s,
unsigned int line_length = 72,
const std::string &prefix =
"") {
387 return word_wrap(s, line_length, prefix, prefix);
415 object_phrase(
int n,
const char *noun,
const char *singlar =
"is",
const char *plural =
"are")
429 std::ostream &
print(std::ostream &os)
const;
437 operator std::string ()
const;
442 const char * m_singular;
443 const char * m_plural;
456 inline std::ostream &operator<<(std::ostream &os,
const object_phrase &phrase) {
457 return phrase.
print(os);
494 typename T::const_iterator lhs_it = lhs.begin();
495 typename T::const_iterator rhs_it = rhs.begin();
496 typename T::const_iterator lhs_it_end = lhs.end();
497 typename T::const_iterator rhs_it_end = rhs.end();
499 for (; lhs_it != lhs_it_end && rhs_it != rhs_it_end; ++lhs_it, ++rhs_it) {
500 int i = std::tolower(*lhs_it) - std::tolower(*rhs_it);
505 if (lhs_it == lhs_it_end)
506 return rhs_it != rhs_it_end;
532 const char * lhs_it = lhs.c_str();
533 const char * rhs_it = rhs.c_str();
534 const char * lhs_it_end = lhs_it + lhs.length();
535 const char * rhs_it_end = rhs_it + rhs.length();
537 for (; lhs_it != lhs_it_end && rhs_it != rhs_it_end; ++lhs_it, ++rhs_it) {
538 int i = std::tolower(*lhs_it) - std::tolower(*rhs_it);
543 if (lhs_it == lhs_it_end)
544 return rhs_it != rhs_it_end;
556 struct less_nocase<
std::string> :
public std::binary_function<std::string, std::string, bool>
569 bool operator()(
const std::string &lhs,
const std::string &rhs)
const {
570 const char * lhs_it = lhs.c_str();
571 const char * rhs_it = rhs.c_str();
572 const char * lhs_it_end = lhs_it + lhs.length();
573 const char * rhs_it_end = rhs_it + rhs.length();
575 for (; lhs_it != lhs_it_end && rhs_it != rhs_it_end; ++lhs_it, ++rhs_it) {
576 int i = std::tolower(*lhs_it) - std::tolower(*rhs_it);
581 if (lhs_it == lhs_it_end)
582 return rhs_it != rhs_it_end;
594 struct less_nocase<const char *> :
public std::binary_function<const char *, const char *, bool>
610 result = NULL != rhs;
612 for (; *lhs && *rhs && std::tolower(*lhs) == std::tolower(*rhs); ++lhs, ++rhs)
614 result = std::tolower(*lhs) < std::tolower(*rhs);
640 typename T::const_iterator lhs_it = lhs.begin();
641 typename T::const_iterator rhs_it = rhs.begin();
642 typename T::const_iterator lhs_it_end = lhs.end();
643 typename T::const_iterator rhs_it_end = rhs.end();
645 for (; lhs_it != lhs_it_end && rhs_it != rhs_it_end; ++lhs_it, ++rhs_it) {
646 if (std::tolower(*lhs_it) != std::tolower(*rhs_it))
650 return lhs_it == lhs_it_end && rhs_it == rhs_it_end;
673 const char * lhs_it = lhs.c_str();
674 const char * rhs_it = rhs.c_str();
675 const char * lhs_it_end = lhs_it + lhs.length();
676 const char * rhs_it_end = rhs_it + rhs.length();
678 for (; lhs_it != lhs_it_end && rhs_it != rhs_it_end; ++lhs_it, ++rhs_it) {
679 if (std::tolower(*lhs_it) != std::tolower(*rhs_it))
683 return lhs_it == lhs_it_end && rhs_it == rhs_it_end;
692 struct equal_nocase<
std::string> :
public std::binary_function<std::string, std::string, bool>
705 bool operator()(
const std::string &lhs,
const std::string &rhs)
const {
706 const char * lhs_it = lhs.c_str();
707 const char * rhs_it = rhs.c_str();
708 const char * lhs_it_end = lhs_it + lhs.length();
709 const char * rhs_it_end = rhs_it + rhs.length();
711 for (; lhs_it != lhs_it_end && rhs_it != rhs_it_end; ++lhs_it, ++rhs_it) {
712 if (std::tolower(*lhs_it) != std::tolower(*rhs_it))
716 return lhs_it == lhs_it_end && rhs_it == rhs_it_end;
725 struct equal_nocase<const char *> :
public std::binary_function<const char *, const char *, bool> {
738 bool result = lhs == rhs ;
739 if ( ! result && NULL != lhs && NULL != rhs ) {
740 for (; *lhs && *rhs && std::tolower(*lhs) == std::tolower(*rhs) ; ++lhs, ++rhs);
741 result = 0 == *lhs && 0 == *rhs ;
765 const size_t sr = std::numeric_limits<unsigned char>::digits *
sizeof(size_t) - 8;
766 const size_t mask = ((size_t) 0xF) << (sr + 4);
768 h = (h << 4) + std::tolower(*p++);
860 #endif // STK_UTIL_DIAG_StringUtil_h size_t operator()(const String &__s) const
Member function operator() returns the hash value of the specified string.
std::string title(const std::string &s)
Function title returns a first letter of each word capitalized of the string.
int to_lower(int c)
Function to_lower returns the value converted to lower case.
std::ostream & print(std::ostream &os) const
Member function print writes the object phrase to the output stream.
Class hash_nocase is a traits class for hash functions.
T & trim(T &name)
Function trim trims preceeding and trailing spaces from name. The convertion happens in place...
object_phrase(int n, const char *noun, const char *singlar="is", const char *plural="are")
const char * demangle(const char *symbol)
Function demangle returns the demangled C++ symbol from the mangled C++ symbol. The mangled named is ...
bool operator()(const std::string &lhs, const std::string &rhs) const
Member function operator() returns true if the lhs is equal to rhs.
T & make_identifier(T &name)
Function make_identifier converts string name to an identifier case. The convertion happens in place...
String lower(const String &s)
Function lower returns a lower case version of the string.
std::istream & getline(std::istream &is, sierra::String &s, char eol)
Function getline returns a string from the input stream which has been terminated by the newline char...
bool operator()(const T &lhs, const T &rhs) const
Member function operator() returns true if the lhs is equal to rhs.
int to_identifier_upper(int c)
Function to_identifier_upper returns the value converted to underscore if it is a space or to upper c...
int to_upper(int c)
Function to_upper returns the value converted to upper case.
Class less_nocase implements a case insensitive compare functor.
T & make_upper(T &name)
Function make_upper converts strgin name to upper case. The conversion happens in place...
T convert_cast(const String &s)
Class specialization hash_nocase for std::string.
std::string format_time(double t, const char *format)
Function format_time encodes the time using the format specified. The format is described in stdftime...
Class object_phrase makes a pretty string for those annoying plural or singular noun/verb phrases...
bool operator()(const T &lhs, const T &rhs) const
Member function operator() returns true if the lhs is less than rhs.
std::string word_wrap(const std::string &s, unsigned int line_length, const std::string &prefix, const std::string &prefix_first_line)
Function word_wrap reformats a string into multiple lines, none longer that line_length, the first line prefixed with prefix_first_line and the remaining lines prefixed with prefix.
int to_identifier_lower(int c)
Function to_identifier_lower returns the value converted to underscore if it is a space or to lower c...
T & make_lower_identifier(T &name)
Function make_lower_identifier converts string name to a lower case identifier case. The convertion happens in place.
bool operator()(const char *lhs, const char *rhs) const
Member function operator() returns true if the lhs is equal to rhs.
Class equal_nocase implements a case insensitive compare functor.
bool operator()(const String &lhs, const String &rhs) const
Member function operator() returns true if the lhs is equal to rhs.
bool operator()(const String &lhs, const String &rhs) const
Member function operator() returns true if the lhs is less than rhs.
Part * find(const PartVector &parts, const std::string &name)
Find a part by name in a collection of parts.
T & make_lower(T &name)
Function make_lower converts string name to lower case. The convertion happens in place...
int case_strcmp(const char *c1, const char *c2)
Function case_strcmp compares two null terminated strings case insenstively. It returns zero if they ...
size_t hash_string_nocase(const char *p)
Function hash_string_nocase hash a character string case insensitively.
bool operator()(const char *lhs, const char *rhs) const
Member function operator() returns true if the lhs is less than rhs.
bool operator()(const std::string &lhs, const std::string &rhs) const
Member function operator() returns true if the lhs is less than rhs.
size_t operator()(const std::string &__s) const
Member function operator() returns the hash value of the specified string.
std::string to_string(const T &t)