Panda3D

string_utils.h

00001 // Filename: string_utils.h
00002 // Created by:  drose (18Jan99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef STRING_UTILS_H
00016 #define STRING_UTILS_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include <string>
00021 #include "vector_string.h"
00022 
00023 // Case-insensitive string comparison, from Stroustrup's C++ third edition.
00024 // Works like strcmp().
00025 EXPCL_PANDA_PUTIL int cmp_nocase(const string &s, const string &s2);
00026 
00027 // Similar, except it also accepts hyphen and underscore as equivalent.
00028 EXPCL_PANDA_PUTIL int cmp_nocase_uh(const string &s, const string &s2);
00029 
00030 // Returns the string converted to lowercase.
00031 EXPCL_PANDA_PUTIL string downcase(const string &s);
00032 
00033 // Returns the string converted to uppercase.
00034 EXPCL_PANDA_PUTIL string upcase(const string &s);
00035 
00036 // Separates the string into words according to whitespace.
00037 EXPCL_PANDA_PUTIL int extract_words(const string &str, vector_string &words);
00038 EXPCL_PANDA_PUTIL int extract_words(const wstring &str, pvector<wstring> &words);
00039 
00040 // Separates the string into words according to the indicated delimiters.
00041 EXPCL_PANDA_PUTIL void tokenize(const string &str, vector_string &words,
00042                           const string &delimiters,
00043                           bool discard_repeated_delimiters = false);
00044 EXPCL_PANDA_PUTIL void tokenize(const wstring &str, pvector<wstring> &words,
00045                           const wstring &delimiters,
00046                           bool discard_repeated_delimiters = false);
00047 
00048 // Trims leading and/or trailing whitespace from the string.
00049 EXPCL_PANDA_PUTIL string trim_left(const string &str);
00050 EXPCL_PANDA_PUTIL wstring trim_left(const wstring &str);
00051 EXPCL_PANDA_PUTIL string trim_right(const string &str);
00052 EXPCL_PANDA_PUTIL wstring trim_right(const wstring &str);
00053 EXPCL_PANDA_PUTIL string trim(const string &str);
00054 EXPCL_PANDA_PUTIL wstring trim(const wstring &str);
00055 
00056 // Functions to parse numeric values out of a string.
00057 EXPCL_PANDA_PUTIL int string_to_int(const string &str, string &tail);
00058 EXPCL_PANDA_PUTIL bool string_to_int(const string &str, int &result);
00059 EXPCL_PANDA_PUTIL double string_to_double(const string &str, string &tail);
00060 EXPCL_PANDA_PUTIL bool string_to_double(const string &str, double &result);
00061 EXPCL_PANDA_PUTIL bool string_to_float(const string &str, PN_stdfloat &result);
00062 
00063 // Convenience function to make a string from anything that has an
00064 // ostream operator.
00065 template<class Thing>
00066 INLINE string format_string(const Thing &thing);
00067 
00068 #include "string_utils.I"
00069 
00070 #endif
00071 
 All Classes Functions Variables Enumerations