summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/astring.c
diff options
context:
space:
mode:
author Aaron Giles <aaron@aarongiles.com>2012-01-03 00:21:13 +0000
committer Aaron Giles <aaron@aarongiles.com>2012-01-03 00:21:13 +0000
commit64f1231c6300b144d946cb5530e40c288c43791f (patch)
tree92abaefa81552cb9ad78ff7c142e0fff5f9f7bba /src/lib/util/astring.c
parent8b492b8d80c5998f1e695706dfaa1bb1d35b9263 (diff)
Removed old C-based interface to astrings. astring exists only as
a class now. Updated all stragglers (mostly tools) to use the class form. [Aaron Giles]
Diffstat (limited to 'src/lib/util/astring.c')
-rw-r--r--src/lib/util/astring.c826
1 files changed, 263 insertions, 563 deletions
diff --git a/src/lib/util/astring.c b/src/lib/util/astring.c
index 9a2507ca313..984848f152f 100644
--- a/src/lib/util/astring.c
+++ b/src/lib/util/astring.c
@@ -44,202 +44,107 @@
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
-
+#include <new>
/***************************************************************************
GLOBAL VARIABLES
***************************************************************************/
-static const char dummy_text[2] = { 0 };
-
-#ifdef __cplusplus
static const astring dummy_astring;
-#else
-static const astring dummy_astring = { (char *)dummy_text, 1, { 0 } };
-#endif
-
-
-
-/***************************************************************************
- INLINE FUNCTIONS
-***************************************************************************/
-
-/*-------------------------------------------------
- ensure_room - ensure we have room for a
- given string, or else reallocate our buffer
--------------------------------------------------*/
-
-INLINE int ensure_room(astring *str, int length)
-{
- char *newbuf, *oldbuf;
- int alloclen;
-
- /* always fail to expand the dummy */
- if (str == &dummy_astring)
- return FALSE;
-
- /* if we have the room, do nothing */
- if (str->alloclen >= length + 1)
- return TRUE;
-
- /* allocate a new buffer with some slop */
- alloclen = length + 256;
- newbuf = (char *)malloc(alloclen);
- if (newbuf == NULL)
- return FALSE;
-
- /* swap in the new buffer and free the old one */
- oldbuf = (str->text == str->smallbuf) ? NULL : str->text;
- str->text = strcpy(newbuf, str->text);
- str->alloclen = alloclen;
- if (oldbuf != NULL)
- free(oldbuf);
-
- return TRUE;
-}
-/*-------------------------------------------------
- safe_string_base - return a "safe" string
- base for a given start index
--------------------------------------------------*/
-INLINE char *safe_string_base(char *base, int start)
-{
- int max = strlen(base);
- return (start >= 0 && start < max) ? base + start : base + max;
-}
+//**************************************************************************
+// INLINE FUNCTIONS
+//**************************************************************************
+//-------------------------------------------------
+// ensure_room - ensure we have room for a
+// given string, or else reallocate our buffer
+//-------------------------------------------------
-/*-------------------------------------------------
- normalize_substr - normalize substr parameters
--------------------------------------------------*/
-
-INLINE void normalize_substr(int *start, int *count, int length)
+inline bool astring::ensure_room(int length)
{
- /* limit start */
- if (*start < 0)
- *start = 0;
- else if (*start > length)
- *start = length;
-
- /* update count */
- if (*count == -1 || *start + *count > length)
- *count = length - *start;
-}
-
-
+ // always fail to expand the dummy
+ if (this == &dummy_astring)
+ return false;
-/***************************************************************************
- ASTRING ALLOCATION
-***************************************************************************/
+ // if we have the room, do nothing
+ if (m_alloclen >= length + 1)
+ return true;
-#ifdef __cplusplus
+ // allocate a new buffer with some slop
+ int alloclen = length + 256;
+ char *newbuf = new char[alloclen];
-#include <new>
+ // swap in the new buffer and free the old one
+ char *oldbuf = (m_text == m_smallbuf) ? NULL : m_text;
+ m_text = strcpy(newbuf, m_text);
+ m_alloclen = alloclen;
+ delete[] oldbuf;
-/*-------------------------------------------------
- init - constructor helper
--------------------------------------------------*/
-
-astring &astring::init()
-{
- text = smallbuf;
- alloclen = ARRAY_LENGTH(smallbuf);
- smallbuf[0] = 0;
- return *this;
+ return true;
}
-/*-------------------------------------------------
- ~astring - basic destructor
--------------------------------------------------*/
+//-------------------------------------------------
+// safe_string_base - return a "safe" string
+// base for a given start index
+//-------------------------------------------------
-astring::~astring()
+inline char *astring::safe_string_base(int start) const
{
- if (text != smallbuf)
- free(text);
+ int max = len();
+ return (start >= 0 && start < max) ? m_text + start : m_text + max;
}
-/*-------------------------------------------------
- astring_alloc - allocate a new astring
--------------------------------------------------*/
+//-------------------------------------------------
+// normalize_substr - normalize substr parameters
+//-------------------------------------------------
-astring *astring_alloc(void)
+inline void astring::normalize_substr(int &start, int &count, int length) const
{
- // we do our own malloc and use placement new here to
- // make our memory tracking happy
- void *ptr = malloc(sizeof(astring));
- if (ptr == NULL)
- return NULL;
- return new(ptr) astring;
-}
+ // limit start
+ if (start < 0)
+ start = 0;
+ else if (start > length)
+ start = length;
-
-/*-------------------------------------------------
- astring_free - free an astring
--------------------------------------------------*/
-
-void astring_free(astring *str)
-{
- // since we malloc'ed the memory ourselves, we directly
- // call the destructor and free the memory ourselves
- str->~astring();
- free(str);
+ // update count
+ if (count == -1 || start + count > length)
+ count = length - start;
}
-#else
-/*-------------------------------------------------
- astring_alloc - allocate a new astring
--------------------------------------------------*/
-astring *astring_alloc(void)
-{
- astring *str;
-
- /* allocate memory; if we fail, return the dummy */
- str = (astring *)malloc(sizeof(*str));
- if (str == NULL)
- return (astring *)&dummy_astring;
- memset(str, 0, sizeof(*str));
-
- /* initialize the small buffer */
- str->text = str->smallbuf;
- str->alloclen = ARRAY_LENGTH(str->smallbuf);
- return str;
-}
+//**************************************************************************
+// ASTRING ALLOCATION
+//**************************************************************************
+//-------------------------------------------------
+// init - constructor helper
+//-------------------------------------------------
-/*-------------------------------------------------
- astring_free - free an astring
--------------------------------------------------*/
-
-void astring_free(astring *str)
+astring &astring::init()
{
- /* ignore attempts to free the dummy */
- if (str == &dummy_astring)
- return;
-
- /* if we allocated additional memory, free that */
- if (str->text != str->smallbuf)
- free(str->text);
- free(str);
+ // initialize ourselves to point to the internal buffer
+ m_text = m_smallbuf;
+ m_alloclen = ARRAY_LENGTH(m_smallbuf);
+ m_smallbuf[0] = 0;
+ return *this;
}
-#endif
-
-/*-------------------------------------------------
- astring_expand - expand an astring to
- guarantee the given amount of space
--------------------------------------------------*/
+//-------------------------------------------------
+// ~astring - destructor
+//-------------------------------------------------
-void astring_expand(astring *str, int length)
+astring::~astring()
{
- ensure_room(str, length);
+ if (m_text != m_smallbuf)
+ delete[] m_text;
}
@@ -248,238 +153,146 @@ void astring_expand(astring *str, int length)
INLINE ASTRING CHANGES
***************************************************************************/
-/*-------------------------------------------------
- astring_cpy - copy one astring into another
--------------------------------------------------*/
-
-astring *astring_cpy(astring *dst, const astring *src)
-{
- return astring_cpyc(dst, src->text);
-}
-
-
-/*-------------------------------------------------
- astring_cpyc - copy a C string into an astring
--------------------------------------------------*/
-
-astring *astring_cpyc(astring *dst, const char *src)
-{
- return astring_cpych(dst, src, strlen(src));
-}
-
-
-/*-------------------------------------------------
- astring_cpych - copy a character array into
- an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// cpy - copy a character array into an astring
+//-------------------------------------------------
-astring *astring_cpych(astring *dst, const char *src, int count)
+astring &astring::cpy(const char *src, int count)
{
- /* make room; if we fail or if dst is the dummy, do nothing */
- if (!ensure_room(dst, count))
- return dst;
-
- /* copy the raw data and NULL-terminate */
- if ((count > 0) && (dst->text != src))
- memcpy(dst->text, src, count);
- dst->text[count] = 0;
- return dst;
-}
-
-
-/*-------------------------------------------------
- astring_cpysubstr - copy a substring of one
- string to another
--------------------------------------------------*/
-
-astring *astring_cpysubstr(astring *dst, const astring *src, int start, int count)
-{
- normalize_substr(&start, &count, strlen(src->text));
- return astring_cpych(dst, src->text + start, count);
-}
-
+ // make room; if we fail or if we are the dummy, do nothing
+ if (!ensure_room(count))
+ return *this;
-/*-------------------------------------------------
- astring_ins - insert one astring into another
--------------------------------------------------*/
-
-astring *astring_ins(astring *dst, int insbefore, const astring *src)
-{
- return astring_insc(dst, insbefore, src->text);
+ // copy the raw data and NULL-terminate
+ if (count > 0 && m_text != src)
+ memcpy(m_text, src, count);
+ m_text[count] = 0;
+ return *this;
}
-/*-------------------------------------------------
- astring_insc - insert a C string into an
- astring
--------------------------------------------------*/
+//-------------------------------------------------
+// cpysubstr - copy a substring of one string to
+// another
+//-------------------------------------------------
-astring *astring_insc(astring *dst, int insbefore, const char *src)
+astring &astring::cpysubstr(const astring &src, int start, int count)
{
- return astring_insch(dst, insbefore, src, strlen(src));
+ normalize_substr(start, count, strlen(src.m_text));
+ return cpy(src.m_text + start, count);
}
-/*-------------------------------------------------
- astring_insch - insert a character array
- into an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// ins - insert a character array into an astring
+//-------------------------------------------------
-astring *astring_insch(astring *dst, int insbefore, const char *src, int count)
+astring &astring::ins(int insbefore, const char *src, int count)
{
- int dstlength = strlen(dst->text);
+ // make room; if we fail or if we are the dummy, do nothing
+ int dstlength = len();
+ if (!ensure_room(dstlength + count))
+ return *this;
- /* make room; if we fail or if dst is the dummy, do nothing */
- if (!ensure_room(dst, dstlength + count))
- return dst;
-
- /* adjust insbefore to be logical */
+ // adjust insbefore to be logical
if (insbefore < 0 || insbefore > dstlength)
insbefore = dstlength;
- /* copy the data an NULL-terminate */
+ // copy the data an NULL-terminate
if (insbefore < dstlength)
- memmove(dst->text + insbefore + count, dst->text + insbefore, dstlength - insbefore);
- memcpy(dst->text + insbefore, src, count);
- dst->text[dstlength + count] = 0;
- return dst;
+ memmove(m_text + insbefore + count, m_text + insbefore, dstlength - insbefore);
+ memcpy(m_text + insbefore, src, count);
+ m_text[dstlength + count] = 0;
+ return *this;
}
-/*-------------------------------------------------
- astring_inssubstr - insert a substring of
- one string to another
--------------------------------------------------*/
+//-------------------------------------------------
+// inssubstr - insert a substring of one string
+// into another
+//-------------------------------------------------
-astring *astring_inssubstr(astring *dst, int insbefore, const astring *src, int start, int count)
+astring &astring::inssubstr(int insbefore, const astring &src, int start, int count)
{
- normalize_substr(&start, &count, strlen(src->text));
- return astring_insch(dst, insbefore, src->text + start, count);
+ normalize_substr(start, count, strlen(src.m_text));
+ return ins(insbefore, src.m_text + start, count);
}
-/*-------------------------------------------------
- astring_substr - extract a substring of
- ourself, removing everything else
--------------------------------------------------*/
+//-------------------------------------------------
+// substr - extract a substring of ourself,
+// removing everything else
+//-------------------------------------------------
-astring *astring_substr(astring *str, int start, int count)
+astring &astring::substr(int start, int count)
{
- /* ignore attempts to do this on the dummy */
- if (str == &dummy_astring)
- return str;
+ // ignore attempts to do this on the dummy
+ if (this == &dummy_astring)
+ return *this;
- /* normalize parameters */
- normalize_substr(&start, &count, strlen(str->text));
+ // normalize parameters
+ normalize_substr(start, count, strlen(m_text));
- /* move the data and NULL-terminate */
+ // move the data and NULL-terminate
if (count > 0 && start > 0)
- memmove(str->text, str->text + start, count);
- str->text[count] = 0;
- return str;
+ memmove(m_text, m_text + start, count);
+ m_text[count] = 0;
+ return *this;
}
-/*-------------------------------------------------
- astring_del - delete a substring of
- ourself, keeping everything else
--------------------------------------------------*/
+//-------------------------------------------------
+// del - delete a substring of ourself, keeping
+// everything else
+//-------------------------------------------------
-astring *astring_del(astring *str, int start, int count)
+astring &astring::del(int start, int count)
{
- int strlength = strlen(str->text);
-
- /* ignore attempts to do this on the dummy */
- if (str == &dummy_astring)
- return str;
+ // ignore attempts to do this on the dummy
+ if (this == &dummy_astring)
+ return *this;
- /* normalize parameters */
- normalize_substr(&start, &count, strlength);
+ // normalize parameters
+ int strlength = strlen(m_text);
+ normalize_substr(start, count, strlength);
- /* move the data and NULL-terminate */
+ // move the data and NULL-terminate
if (count > 0)
- memmove(str->text + start, str->text + start + count, strlength - (start + count));
- str->text[strlength - count] = 0;
- return str;
-}
-
-
-/*-------------------------------------------------
- astring_printf - printf text into an astring
--------------------------------------------------*/
-
-int astring_printf(astring *dst, const char *format, ...)
-{
- char tempbuf[4096];
- va_list args;
- int result;
-
- /* sprintf into the temporary buffer */
- va_start(args, format);
- result = vsprintf(tempbuf, format, args);
- va_end(args);
-
- /* set the result */
- astring_cpyc(dst, tempbuf);
- return result;
-}
-
-
-/*-------------------------------------------------
- astring_vprintf - vprintf text into an astring
--------------------------------------------------*/
-
-int astring_vprintf(astring *dst, const char *format, va_list args)
-{
- char tempbuf[4096];
- int result;
-
- /* sprintf into the temporary buffer */
- result = vsprintf(tempbuf, format, args);
-
- /* set the result */
- astring_cpyc(dst, tempbuf);
- return result;
+ memmove(m_text + start, m_text + start + count, strlength - (start + count));
+ m_text[strlength - count] = 0;
+ return *this;
}
-/*-------------------------------------------------
- astring_catprintf - formatted printf to
- the end of an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// vprintf - vprintf text into an astring
+//-------------------------------------------------*/
-int astring_catprintf(astring *dst, const char *format, ...)
+int astring::vprintf(const char *format, va_list args)
{
+ // sprintf into the temporary buffer
char tempbuf[4096];
- va_list args;
- int result;
-
- /* sprintf into the temporary buffer */
- va_start(args, format);
- result = vsprintf(tempbuf, format, args);
- va_end(args);
+ int result = vsprintf(tempbuf, format, args);
- /* append the result */
- astring_catc(dst, tempbuf);
+ // set the result
+ cpy(tempbuf);
return result;
}
-/*-------------------------------------------------
- astring_catprintf - formatted vprintf to
- the end of an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// catprintf - formatted vprintf to the end of
+// an astring
+//-------------------------------------------------
-int astring_catvprintf(astring *dst, const char *format, va_list args)
+int astring::catvprintf(const char *format, va_list args)
{
+ // sprintf into the temporary buffer
char tempbuf[4096];
- int result;
+ int result = vsprintf(tempbuf, format, args);
- /* sprintf into the temporary buffer */
- result = vsprintf(tempbuf, format, args);
-
- /* append the result */
- astring_catc(dst, tempbuf);
+ // append the result
+ cat(tempbuf);
return result;
}
@@ -489,316 +302,203 @@ int astring_catvprintf(astring *dst, const char *format, va_list args)
ASTRING QUERIES
***************************************************************************/
-/*-------------------------------------------------
- astring_c - return a pointer to a C string
- from an astring
--------------------------------------------------*/
-
-const char *astring_c(const astring *str)
-{
- return str->text;
-}
-
-
-/*-------------------------------------------------
- astring_len - return the length of an astring
--------------------------------------------------*/
-
-int astring_len(const astring *str)
-{
- return strlen(str->text);
-}
-
-
-/*-------------------------------------------------
- astring_cmp - compare one astring to another
--------------------------------------------------*/
+//-------------------------------------------------
+// cmp - compare a character array to an astring
+//-------------------------------------------------
-int astring_cmp(const astring *str1, const astring *str2)
+int astring::cmp(const char *str2, int count) const
{
- return astring_cmpc(str1, str2->text);
-}
-
-
-/*-------------------------------------------------
- astring_cmpc - compare a C string to an astring
--------------------------------------------------*/
+ // loop while equal until we hit the end of strings
+ int index;
+ for (index = 0; index < count; index++)
+ if (m_text[index] == 0 || m_text[index] != str2[index])
+ break;
-int astring_cmpc(const astring *str1, const char *str2)
-{
- const char *s1 = str1->text;
-
- /* loop while equal until we hit the end of strings */
- while (*s1 != 0 && *str2 != 0 && *s1 == *str2)
- s1++, str2++;
- return *s1 - *str2;
+ // determine the final result
+ if (index < count)
+ return m_text[index] - str2[index];
+ if (m_text[index] == 0)
+ return 0;
+ return 1;
}
-/*-------------------------------------------------
- astring_cmpch - compare a character array to
- an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// cmpsubstr - compare a substring to an astring
+//-------------------------------------------------
-int astring_cmpch(const astring *str1, const char *str2, int count)
+int astring::cmpsubstr(const astring &str2, int start, int count) const
{
- const char *s1 = str1->text;
- int result;
-
- /* loop while equal until we hit the end of strings */
- while (count-- > 0 && *s1 != 0 && *str2 != 0 && *s1 == *str2)
- s1++, str2++;
- result = (count == -1) ? 0 : *s1 - *str2;
- if (result == 0 && *s1 != 0)
- result = 1;
- return result;
+ normalize_substr(start, count, strlen(str2.m_text));
+ return cmp(str2.m_text + start, count);
}
-/*-------------------------------------------------
- astring_cmpsubstr - compare a substring to
- an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// icmp - compare a character array to an astring
+//-------------------------------------------------
-int astring_cmpsubstr(const astring *str1, const astring *str2, int start, int count)
+int astring::icmp(const char *str2, int count) const
{
- normalize_substr(&start, &count, strlen(str2->text));
- return astring_cmpch(str1, str2->text + start, count);
-}
+ // loop while equal until we hit the end of strings
+ int index;
+ for (index = 0; index < count; index++)
+ if (m_text[index] == 0 || tolower(m_text[index]) != tolower(str2[index]))
+ break;
-
-/*-------------------------------------------------
- astring_icmp - case-insenstive compare one
- astring to another
--------------------------------------------------*/
-
-int astring_icmp(const astring *str1, const astring *str2)
-{
- return astring_icmpc(str1, str2->text);
+ // determine the final result
+ if (index < count)
+ return tolower(m_text[index]) - tolower(str2[index]);
+ if (m_text[index] == 0)
+ return 0;
+ return 1;
}
-/*-------------------------------------------------
- astring_icmpc - case-insenstive compare a C
- string to an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// icmpsubstr - compare a substring to an astring
+//-------------------------------------------------
-int astring_icmpc(const astring *str1, const char *str2)
+int astring::icmpsubstr(const astring &str2, int start, int count) const
{
- const char *s1 = str1->text;
-
- /* loop while equal until we hit the end of strings */
- while (*s1 != 0 && *str2 != 0 && tolower((UINT8)*s1) == tolower((UINT8)*str2))
- s1++, str2++;
- return tolower((UINT8)*s1) - tolower((UINT8)*str2);
+ normalize_substr(start, count, strlen(str2.m_text));
+ return icmp(str2.m_text + start, count);
}
-/*-------------------------------------------------
- astring_icmpch - case-insenstive compare a
- character array to an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// chr - return the index of a character in an
+// astring
+//-------------------------------------------------
-int astring_icmpch(const astring *str1, const char *str2, int count)
+int astring::chr(int start, int ch) const
{
- const char *s1 = str1->text;
- int result;
-
- /* loop while equal until we hit the end of strings */
- while (count-- > 0 && *s1 != 0 && *str2 != 0 && tolower((UINT8)*s1) == tolower((UINT8)*str2))
- s1++, str2++;
- result = (count == -1) ? 0 : tolower((UINT8)*s1) - tolower((UINT8)*str2);
- if (result == 0 && *s1 != 0)
- result = 1;
- return result;
+ char *result = strchr(safe_string_base(start), ch);
+ return (result != NULL) ? (result - m_text) : -1;
}
-/*-------------------------------------------------
- astring_icmpsubstr - case-insenstive compare a
- substring to an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// rchr - return the index of a character in an
+// astring, searching from the end
+//-------------------------------------------------
-int astring_icmpsubstr(const astring *str1, const astring *str2, int start, int count)
+int astring::rchr(int start, int ch) const
{
- normalize_substr(&start, &count, strlen(str2->text));
- return astring_icmpch(str1, str2->text + start, count);
+ char *result = strrchr(safe_string_base(start), ch);
+ return (result != NULL) ? (result - m_text) : -1;
}
-/*-------------------------------------------------
- astring_chr - return the index of a character
- in an astring
--------------------------------------------------*/
+//-------------------------------------------------
+// find - find a C string in an astring
+//-------------------------------------------------*/
-int astring_chr(const astring *str, int start, int ch)
+int astring::find(int start, const char *search) const
{
- char *result = strchr(safe_string_base(str->text, start), ch);
- return (result != NULL) ? (result - str->text) : -1;
+ char *result = strstr(safe_string_base(start), search);
+ return (result != NULL) ? (result - m_text) : -1;
}
-/*-------------------------------------------------
- astring_rchr - return the index of a character
- in an astring, searching from the end
--------------------------------------------------*/
+//-------------------------------------------------
+// replacec - search in an astring for a C string,
+// replacing all instances with another C string
+// and returning the number of matches
+//-------------------------------------------------
-int astring_rchr(const astring *str, int start, int ch)
-{
- char *result = strrchr(safe_string_base(str->text, start), ch);
- return (result != NULL) ? (result - str->text) : -1;
-}
-
-
-/*-------------------------------------------------
- astring_find - find one astring in another
--------------------------------------------------*/
-
-int astring_find(const astring *str, int start, const astring *search)
-{
- return astring_findc(str, start, search->text);
-}
-
-
-/*-------------------------------------------------
- astring_findc - find a C string in an astring
--------------------------------------------------*/
-
-int astring_findc(const astring *str, int start, const char *search)
-{
- char *result = strstr(safe_string_base(str->text, start), search);
- return (result != NULL) ? (result - str->text) : -1;
-}
-
-
-/*-------------------------------------------------
- astring_replace - search in an astring for
- another astring, replacing all instances with
- a third and returning the number of matches
--------------------------------------------------*/
-
-int astring_replace(astring *str, int start, const astring *search, const astring *replace)
-{
- return astring_replacec(str, start, search->text, replace->text);
-}
-
-
-/*-------------------------------------------------
- astring_replacec - search in an astring for a
- C string, replacing all instances with another
- C string and returning the number of matches
--------------------------------------------------*/
-
-int astring_replacec(astring *str, int start, const char *search, const char *replace)
+int astring::replace(int start, const char *search, const char *replace)
{
int searchlen = strlen(search);
int replacelen = strlen(replace);
int matches = 0;
- int curindex;
- for (curindex = astring_findc(str, start, search); curindex != -1; curindex = astring_findc(str, curindex + replacelen, search))
+ for (int curindex = find(start, search); curindex != -1; curindex = find(curindex + replacelen, search))
{
matches++;
- astring_del(str, curindex, searchlen);
- astring_insc(str, curindex, replace);
+ del(curindex, searchlen).ins(curindex, replace);
}
return matches;
}
-/***************************************************************************
- ASTRING UTILITIES
-***************************************************************************/
+//**************************************************************************
+// ASTRING UTILITIES
+//**************************************************************************
-/*-------------------------------------------------
- astring_delchr - delete all instances of
- 'ch'
--------------------------------------------------*/
+//-------------------------------------------------
+// delchr - delete all instances of 'ch'
+//-------------------------------------------------
-astring *astring_delchr(astring *str, int ch)
+astring &astring::delchr(int ch)
{
- char *src, *dst;
-
- /* simple deletion */
- for (src = dst = str->text; *src != 0; src++)
+ // simple deletion
+ char *dst = m_text;
+ for (char *src = m_text; *src != 0; src++)
if (*src != ch)
*dst++ = *src;
*dst = 0;
-
- return str;
+ return *this;
}
-/*-------------------------------------------------
- astring_replacechr - replace all instances of
- 'ch' with 'newch'
--------------------------------------------------*/
+//-------------------------------------------------
+// replacechr - replace all instances of 'ch'
+// with 'newch'
+//-------------------------------------------------
-astring *astring_replacechr(astring *str, int ch, int newch)
+astring &astring::replacechr(int ch, int newch)
{
- char *text;
-
- /* simple replacement */
- for (text = str->text; *text != 0; text++)
+ // simple replacement
+ for (char *text = m_text; *text != 0; text++)
if (*text == ch)
*text = newch;
-
- return str;
+ return *this;
}
-/*-------------------------------------------------
- astring_toupper - convert string to all
- upper-case
--------------------------------------------------*/
+//-------------------------------------------------
+// makeupper - convert string to all upper-case
+//-------------------------------------------------
-astring *astring_toupper(astring *str)
+astring &astring::makeupper()
{
- char *text;
-
- /* just toupper() on all characters */
- for (text = str->text; *text != 0; text++)
+ // just makeupper() on all characters
+ for (char *text = m_text; *text != 0; text++)
*text = toupper((UINT8)*text);
-
- return str;
+ return *this;
}
-/*-------------------------------------------------
- astring_tolower - convert string to all
- lower-case
--------------------------------------------------*/
+//-------------------------------------------------
+// makelower - convert string to all lower-case
+//-------------------------------------------------
-astring *astring_tolower(astring *str)
+astring &astring::makelower()
{
- char *text;
-
- /* just tolower() on all characters */
- for (text = str->text; *text != 0; text++)
+ // just tolower() on all characters
+ for (char *text = m_text; *text != 0; text++)
*text = tolower((UINT8)*text);
-
- return str;
+ return *this;
}
-/*-------------------------------------------------
- astring_trimspace - remove all space
- characters from beginning/end
--------------------------------------------------*/
+//-------------------------------------------------
+// trimspace - remove all space characters from
+// beginning/end
+//-------------------------------------------------
-astring *astring_trimspace(astring *str)
+astring &astring::trimspace()
{
- char *ptr;
-
- /* first remove stuff from the end */
- for (ptr = str->text + strlen(str->text) - 1; ptr >= str->text && (!(*ptr & 0x80) && isspace((UINT8)*ptr)); ptr--)
+ // first remove stuff from the end
+ for (char *ptr = m_text + strlen(m_text) - 1; ptr >= m_text && (!(*ptr & 0x80) && isspace(UINT8(*ptr))); ptr--)
*ptr = 0;
- /* then count how much to remove from the beginning */
- for (ptr = str->text; *ptr != 0 && (!(*ptr & 0x80) && isspace((UINT8)*ptr)); ptr++) ;
- if (ptr > str->text)
- astring_substr(str, ptr - str->text, -1);
-
- return str;
+ // then count how much to remove from the beginning
+ char *ptr;
+ for (ptr = m_text; *ptr != 0 && (!(*ptr & 0x80) && isspace(UINT8(*ptr))); ptr++) ;
+ if (ptr > m_text)
+ substr(ptr - m_text);
+ return *this;
}