]>
Dogcows Code - chaz/yoink/blob - src/moof/string.hh
4194abb08e1679d959b1a5cf58292df366ac241c
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
10 #ifndef _MOOF_STRING_HH_
11 #define _MOOF_STRING_HH_
15 * Functions and classes related to string manipulation.
20 #include <boost/noncopyable.hpp>
30 * Convert a multi-byte (UTF-8) string to a wide string.
31 * \param multi The multi-byte string to convert.
32 * \return The equivalent wide string.
34 wstring
multi_to_wide(const string
& multi
);
37 * Convert a wide string to a multi-byte (UTF-8) string.
38 * \param wide The wide string to convert.
39 * \return The equivalent multi-byte string.
41 string
wide_to_multi(const wstring
& wide
);
45 * Class exposing the pattern-matching and substitution methods used in
48 class pattern
: public boost::noncopyable
53 * Construct a pattern object.
58 * Construct a pattern object with a pattern.
59 * \param pattern The pattern.
61 pattern(const std::string
& pattern
);
64 * Construct a pattern object with a pattern and source to match.
65 * \param pattern The pattern.
66 * \param source The source string.
68 pattern(const std::string
& pattern
, const std::string
& source
);
71 * Deconstruct the pattern.
76 * Get the pattern pattern.
78 std::string
string() const;
81 * Set the pattern string.
83 void string(const std::string
& pattern
);
86 * Match a string against the pattern iteratively.
87 * \param source The source string.
89 void match(const std::string
& source
);
92 * Get the next match. If the pattern contains captures, this version
93 * will only get the first capture.
94 * \param match Reference to a string to be assigned the match.
95 * \return True if there was a match to get, false otherwise.
97 bool get(std::string
& match
);
100 * Get the next match. Use this version if the pattern contains more
101 * than one capture to get all of the captures.
102 * \param captures Reference to a vector of strings to hold the result.
103 * \return True if there was a match to get, false otherwise.
105 bool get(std::vector
<std::string
>& captures
);
108 * Match a string against a pattern all at one time.
109 * \param pattern The pattern.
110 * \param source The source string.
111 * \param position The index of the first character of source to match.
115 match(const std::string
& pattern
, const std::string
& source
,
119 pattern::match(match
, pattern
, source
, position
);
124 * Match a string against a pattern all at one time.
125 * \param match A reference to a string to be assigned the match.
126 * \param pattern The pattern.
127 * \param source The source string.
128 * \param position The index of the first character of source to match.
129 * \return True if a match was made, false otherwise.
131 static bool match(std::string
& match
, const std::string
& pattern
,
132 const std::string
& source
, int position
= 0);
135 * Match a string against a pattern all at one time. If the pattern
136 * contains captures, the resulting vector will contain all of the
138 * \param captures A reference to a vector of strings to contain the
140 * \param pattern The pattern.
141 * \param source The source string.
142 * \param position The index of the first character of source to match.
143 * \return True if a match was made, false otherwise.
145 static bool match(std::vector
<std::string
>& captures
, const std::string
& pattern
,
146 const std::string
& source
, int position
= 0);
149 * Substitute a string using a pattern and replacement string.
150 * \param pattern The pattern.
151 * \param source The source string.
152 * \param replacement The replacement string; may also contain
153 * references to captures.
154 * \return The string with any substitutions made.
156 static std::string
sub(const std::string
& pattern
,
157 const std::string
& source
, const std::string
& replacement
)
159 std::string substitution
;
160 pattern::sub(substitution
, pattern
, source
, replacement
);
165 * Substitute a string using a pattern and replacement string.
166 * \param substitution A reference to a string to contain the result.
167 * \param pattern The pattern.
168 * \param source The source string.
169 * \param replacement The replacement string; may also contain
170 * references to captures.
171 * \return The number of substitutions made.
173 static int sub(std::string
& substitution
, const std::string
& pattern
,
174 const std::string
& source
, const std::string
& replacement
);
180 #endif // _MOOF_STRING_HH_
This page took 0.039106 seconds and 4 git commands to generate.