10 #define __WVSTRUTILS_H 12 #include <sys/types.h> 16 #include "wvstringlist.h" 79 void replace_char(
void *
string,
char c1,
char c2,
int length);
91 char *
strlwr(
char *
string);
97 char *
strupr(
char *
string);
102 bool is_word(
const char *
string);
223 ROUND_DOWN_AT_POINT_FIVE,
224 ROUND_UP_AT_POINT_FIVE,
233 WvString sizetoa(
unsigned long long blocks,
unsigned long blocksize = 1,
234 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
241 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
249 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
256 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
267 int lookup(
const char *str,
const char *
const *table,
268 bool case_sensitive =
false);
277 template<
class StringCollection>
279 const char *splitchars =
" \t",
int limit = 0)
282 char *sptr = s.
edit(), *eptr, oldc;
288 coll.add(emptyString,
true);
292 bool firstrun =
true;
294 while (sptr && *sptr)
304 sptr += strspn(sptr, splitchars);
309 eptr = sptr + strcspn(sptr, splitchars);
313 eptr = sptr + strlen(sptr);
320 coll.add(newstr,
true);
341 template<
class StringCollection>
343 const char *splitchars =
" \t",
int limit = 0)
346 char *cur = s.
edit();
359 int len = strcspn(cur, splitchars);
366 if (!cur[len])
break;
372 #ifndef _WIN32 // don't have regex on win32 380 template<
class StringCollection>
382 const WvRegex ®ex,
int limit = 0)
385 int match_start, match_end;
388 while ((limit == 0 || count < limit)
393 int len = match_start;
394 substr->setsize(len+1);
395 memcpy(substr->
edit(), &s[start], len);
396 substr->
edit()[len] =
'\0';
397 coll.add(substr,
true);
402 if (limit == 0 || count < limit)
406 coll.add(last,
true);
417 template<
class StringCollection>
419 const char *joinchars =
" \t")
421 size_t joinlen = strlen(joinchars);
423 typename StringCollection::Iter s(
424 const_cast<StringCollection&>(coll));
425 for (s.rewind(); s.next(); )
428 totlen += strlen(s->cstr());
434 total.setsize(totlen);
436 char *te = total.
edit();
439 for (s.rewind(); s.next(); )
444 strcat(te, joinchars);
446 strcat(te, s->cstr());
508 for (
const char *p = str; *p; ++p)
512 n = n * T(10) + T(*p -
'0');
514 else if ((
const char *)str == p
591 static inline bool is_int(
const char *str)
603 if (!isdigit(*str++))
613 #endif // __WVSTRUTILS_H A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
WvString diff_dates(time_t t1, time_t t2)
Returns the difference between to dates in a human readable format.
WvString intl_date(time_t _when=-1)
Return the local date (in format of ISO 8601) out of _when.
WvString rfc822_date(time_t _when=-1)
Returns an RFC822-compatible date made out of _when, or, if _when < 0, out of the current time...
char * strupr(char *string)
In-place modify a character string so that all contained letters are in upper case.
char * snip_string(char *haystack, char *needle)
Snip off the first part of 'haystack' if it consists of 'needle'.
bool is_word(const char *string)
Returns true if all characters in 'string' are isalnum() (alphanumeric).
WvString & unique()
make the buf and str pointers owned only by this WvString.
WvString undupe(WvStringParm s, char c)
Replace any consecutive instances of character c with a single one.
char * terminate_string(char *string, char c)
Add character c to the end of a string after removing terminating carriage returns/linefeeds if any...
WvString cstr_escape(const void *data, size_t size, const CStrExtraEscape extra_escapes[]=NULL)
Converts data into a C-style string constant.
WvString depunctuate(WvStringParm line)
Removes any trailing punctuation ('.
WvString hexdump_buffer(const void *buf, size_t len, bool charRep=true)
Produce a hexadecimal dump of the data buffer in 'buf' of length 'len'.
WvString ptr2str(void *ptr)
Converts a pointer into a string, like glibc's p formatter would do.
void strcoll_split(StringCollection &coll, WvStringParm _s, const char *splitchars=" \t", int limit=0)
Splits a string and adds each substring to a collection.
WvString metriculate(const off_t i)
Inserts SI-style spacing into a number (eg passing 9876543210 returns "9 876 543 210") ...
void strcoll_splitstrict(StringCollection &coll, WvStringParm _s, const char *splitchars=" \t", int limit=0)
Splits a string and adds each substring to a collection.
char * non_breaking(const char *string)
Replaces all whitespace characters in the string with non-breaking spaces ( ) for use with web stuff...
WvString fqdomainname()
Get the fqdn of the local host, using gethostbyname() and gethostname()
bool cstr_unescape(WvStringParm cstr, void *data, size_t max_size, size_t &size, const CStrExtraEscape extra_escapes[]=NULL)
Converts a C-style string constant into data.
WvString sizektoa(unsigned long long kbytes, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a size in kilobyes, return a human readable size.
WvString wvgetcwd()
Get the current working directory without a fixed-length buffer.
WvString backslash_escape(WvStringParm s1)
Returns a string with a backslash in front of every non alphanumeric character in s1...
WvString sizeitoa(unsigned long long blocks, unsigned long blocksize=1, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a number of blocks and a blocksize (default==1 byte), return a WvString containing a human-read...
bool continuable_match(WvStringParm string, int &match_start, int &match_end, WVREGEX_REGS_DECL) const
Match a given string against the compiled regular expression, capturing the start and end positions o...
WvRegex – Unified support for regular expressions.
int strcount(WvStringParm s, const char c)
How many times does 'c' occur in "s"?
char * edit()
make the string editable, and return a non-const (char*)
WvString intl_datetime(time_t _when=-1)
Return the local date and time (in format of ISO 8601) out of _when.
WvString intl_time(time_t _when=-1)
Return the local time (in format of ISO 8601) out of _when.
WvString sizetoa(unsigned long long blocks, unsigned long blocksize=1, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a number of blocks and a blocksize (default==1 byte), return a WvString containing a human-read...
WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b)
Replace any instances of "a" with "b" in "s".
WvString spacecat(WvStringParm a, WvStringParm b, char sep= ' ', bool onesep=false)
return the string formed by concatenating string 'a' and string 'b' with the 'sep' character between ...
WvString strcoll_join(const StringCollection &coll, const char *joinchars=" \t")
Concatenates all strings in a collection and returns the result.
WvString url_decode(WvStringParm str, bool no_space=false)
Converts escaped characters (things like %20 etc.) from web URLS into their normal ASCII representati...
WvString secondstoa(unsigned int total_seconds)
Given a number of seconds, returns a formatted human-readable string saying how long the period is...
WvString rfc1123_date(time_t _when)
Returns an RFC1123-compatible date made out of _when.
WvString hostname()
Do gethostname() without a fixed-length buffer.
WvString getfilename(WvStringParm fullname)
Take a full path/file name and splits it up into respective pathname and filename.
WvString passwd_md5(const char *str)
Similar to crypt(), but this randomly selects its own salt.
WvString passwd_crypt(const char *str)
Similar to crypt(), but this randomly selects its own salt.
WvString sizekitoa(unsigned long long kbytes, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a size in kilobytes, return a human readable size.
void replace_char(void *string, char c1, char c2, int length)
Replace all instances of c1 with c2 for the first 'length' characters in 'string'.
int lookup(const char *str, const char *const *table, bool case_sensitive=false)
Finds a string in an array and returns its index.
WvString beforestr(WvStringParm line, WvStringParm a)
Returns everything in line (exclusively) before 'a'.
time_t intl_gmtoff(time_t t)
Return the number of seconds by which localtime (at the given timestamp) is offset from GMT...
char * trim_string(char *string)
Trims whitespace from the beginning and end of the character string, including carriage return / line...
bool isnewline(char c)
Returns true if 'c' is a newline or carriage return character.
WvString is an implementation of a simple and efficient printable-string class.
WvString encode_hostname_as_DN(WvStringParm hostname)
Example: encode_hostname_as_DN("www.fizzle.com") will result in dc=www,dc=fizzle,dc=com,cn=www.fizzle.com.
WvString nice_hostname(WvStringParm name)
Given a hostname, turn it into a "nice" one.
WvString substr(WvString line, unsigned int pos, unsigned int len)
Returns the string of length len starting at pos in line.
char * strlwr(char *string)
In-place modify a character string so that all contained letters are in lower case.
WvString afterstr(WvStringParm line, WvStringParm a)
Returns everything in line (exclusively) after a.
WvString url_encode(WvStringParm str, WvStringParm unsafe="")
Converts all those pesky spaces, colons, and other nasties into nice unreadable Quasi-Unicode codes...
Hex functions for compatibility with older code.
WvString local_date(time_t _when=-1)
Return the local date (TZ applied) out of _when.