home *** CD-ROM | disk | FTP | other *** search
-
- #include <string.h>
- #include <stdlib.h>
- #include <limits.h>
-
- #include "NString.h"
- #include "NString_Misc.h"
-
- //_________________________________________________________________________________
-
- char *NString::string (void) const
- {
- char *result;
-
- if ((result = (char *)malloc(sb->len+1)) == NULL)
- OUT_OF_MEM("string (void) const");
- strcpy(result, sb->str);
- return (result);
- }
-
- //_________________________________________________________________________________
-
- NString NString::fromto (unsigned long int from, unsigned long int to) const
- {
- unsigned long int new_length;
-
- if (sb->len == 0) // am I an empty string ?
- return (*this);
- if ((from > to) || (from > sb->len)) // empty range specified or range outside of NString ?
- return NString();
- if (to > sb->len) // "to" too big ?
- to = sb->len - 1;
- else
- if (to > 0)
- to--;
- else
- return NString();
-
- if (from > 0)
- from--;
-
- if ((new_length = to - from + 1) == sb->len)
- return (*this);
- else
- {
- NString result(new_length);
-
- strncpy(result.sb->str, sb->str+from, new_length);
- return (result);
- }
- }
-
- //_________________________________________________________________________________
-
- NString& NString::cut(unsigned long int at, unsigned long int n)
- {
- const char *fname = "cut (unsigned long int, unsigned long int)";
- unsigned long int final_part;
- unsigned long int new_length;
-
- if (at > 0)
- at--;
- else
- if (n > 0)
- n--;
-
- if ((sb->len == 0) || (n == 0) || (at >= sb->len))
- return (*this);
-
- if ((final_part = at + n) < at) // test for overflow
- {
- final_part = ULONG_MAX;
- new_length = at;
- }
- else
- new_length = (final_part >= sb->len) ? at : sb->len - n;
-
- if (sb->refs > 1)
- {
- strbody *old_sb = sb;
-
- if (! GetNewSB(new_length))
- OUT_OF_MEM(fname);
- if (at > 0)
- memcpy(sb->str, old_sb->str, at); // copy initial remaining part of string
- if (final_part < old_sb->len)
- strcpy(sb->str + at, old_sb->str + final_part); // copy final remaining part of string
- }
- else
- {
- if (final_part < sb->len) // there is a final part to move
- memmove(sb->str + at, sb->str + final_part, sb->len - final_part + 1);
- else
- sb->str[at] = '\0'; // otherwise terminate string after initial part
-
- sb->len = new_length;
-
- if (! ReallocStrBuf(new_length)) // shrink string's memory block
- OUT_OF_MEM(fname); // this "Out of Memory" should never happen ...
- }
- return (*this);
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::leftpos(const char *s) const
- {
- char *pos;
-
- if ((pos = strstr(sb->str, s)) != NULL)
- return (unsigned long int)(pos - sb->str + 1);
- else
- return (0);
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::leftpos(const char c) const
- {
- unsigned long int i, pos = 0;
-
- if (sb->len == 0)
- return (0); // no character can be found in an empty string
-
- for (i=0; (i < sb->len) && (pos == 0); i++)
- if (sb->str[i] == c)
- pos = i+1;
-
- return (pos);
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::leftpos (const Alphabet& a) const
- {
- unsigned long int i, pos = 0;
-
- if (sb->len == 0)
- return (0); // no character can be found in an empty string
-
- for (i=0; (i < sb->len) && (pos == 0); i++)
- if (a.contains(sb->str[i]))
- pos = i+1;
-
- return (pos);
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::rightpos(const char *s) const
- {
- unsigned long int i, pos = 0, s_len = strlen(s);
-
- if (s_len == 0)
- return (sb->len + 1); // an empty substring can be found everywhere
- if (s_len > sb->len)
- return (0); // substring longer than entire NString
- for (i=sb->len - s_len + 1; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (sb->str[i-1] == s[0]) // so the range for i has been offset by +1
- if (strncmp(sb->str + i-1, s, s_len) == 0)
- pos = i;
-
- return (pos);
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::rightpos(const char c) const
- {
- unsigned long int i, pos = 0;
-
- if (sb->len == 0)
- return (0); // no character can be found in an empty string
-
- for (i=sb->len; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (sb->str[i-1] == c) // so the range for i has been offset by +1
- pos = i;
-
- return (pos);
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::rightpos(const Alphabet& a) const
- {
- unsigned long int i, pos = 0;
-
- if (sb->len == 0)
- return (0); // no character can be found in an empty string
-
- for (i=sb->len; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (a.contains(sb->str[i-1])) // so the range for i has been offset by +1
- pos = i;
-
- return (pos);
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftof (const char *context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos-1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftof (const char context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos-1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftof (const NString& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos-1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftof (const Alphabet& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos-1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atleft (const char *context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atleft (const char context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atleft (const NString& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atleft (const Alphabet& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toleft (const char *context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos + strlen(context) - 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toleft (const char context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toleft (const NString& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos + context.sb->len - 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toleft (const Alphabet& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (to(pos)); // the length of an alphabet-context is always 1 letter
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftcut (const char *context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos + strlen(context)));
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftcut (const char context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos + 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftcut (const NString& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos + context.sb->len));
- }
-
- //_________________________________________________________________________________
-
- NString NString::leftcut (const Alphabet& context) const
- {
- unsigned long int pos = leftpos(context);
-
- if (pos == 0)
- return NString();
-
- return (from(pos + 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightof (const char *context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos + strlen(context)));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightof (const char context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos + 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightof (const NString& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos + context.sb->len));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightof (const Alphabet& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos + 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atright (const char *context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atright (const char context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atright (const NString& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::atright (const Alphabet& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return (*this);
-
- return (from(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toright (const char *context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos + strlen(context) - 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toright (const char context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toright (const NString& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos + context.sb->len - 1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::toright (const Alphabet& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightcut (const char *context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos -1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightcut (const char context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos -1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightcut (const NString& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos -1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::rightcut (const Alphabet& context) const
- {
- unsigned long int pos = rightpos(context);
-
- if (pos == 0)
- return NString();
-
- return (to(pos -1));
- }
-
- //_________________________________________________________________________________
-
- int NString::endswith(const char *context) const
- {
- unsigned long int ctx_len = strlen(context);
-
- if (ctx_len == 0)
- return (1); // an empty substring can be found everywhere
- if (ctx_len > sb->len)
- return (0); // context longer than entire NString
- return ((strncmp(sb->str + (sb->len - ctx_len), context, ctx_len)) ? 0 : 1);
- }
-
- //_________________________________________________________________________________
-
- NString span (const char context, const NString& s)
- {
- char *ctx = "x";
-
- ctx[0] = context;
- return (s.to(strspn(s.sb->str, ctx)));
- }
-
- //_________________________________________________________________________________
-
- NString span (const Alphabet& context, const NString& s)
- {
- unsigned long int i;
-
- for (i = 0; (i < s.sb->len) && (context.contains(s.sb->str[i])); i++);
- return (s.to(i));
- }
-
- //_________________________________________________________________________________
-
- NString span (const NString& s, const char context)
- {
- unsigned long int i, pos = 0;
-
- if (s.sb->len == 0)
- return (s); // no character can be found in an empty string
-
- for (i=s.sb->len; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (s.sb->str[i-1] != context) // so the range for i has been offset by +1
- pos = i;
-
- return (s.from(pos+1));
- }
-
- //_________________________________________________________________________________
-
- NString span (const NString& s, const Alphabet& context)
- {
- unsigned long int i, pos = 0;
-
- if (s.sb->len == 0)
- return (s); // no character can be found in an empty string
-
- for (i=s.sb->len; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (! context.contains(s.sb->str[i-1])) // so the range for i has been offset by +1
- pos = i;
-
- return (s.from(pos+1));
- }
-
- //_________________________________________________________________________________
-
- NString operator- (const char c, const NString& s)
- {
- char *ctx = "x";
-
- ctx[0] = c;
- return (s.from(strspn(s.sb->str, ctx)+1));
- }
-
- //_________________________________________________________________________________
-
- NString operator- (const Alphabet& ctx, const NString& s)
- {
- unsigned long int i;
-
- for (i=0; (i < s.sb->len) && (ctx.contains(s.sb->str[i])); i++);
- return (s.from(i+1));
- }
-
- //_________________________________________________________________________________
-
- NString NString::operator- (const char c) const
- {
- unsigned long int i, pos = 0;
-
- if (sb->len == 0)
- return (*this); // no character can be found in an empty string
-
- for (i=sb->len; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (sb->str[i-1] != c) // so the range for i has been offset by +1
- pos = i;
-
- return (to(pos));
- }
-
- //_________________________________________________________________________________
-
- NString NString::operator- (const Alphabet& ctx) const
- {
- unsigned long int i, pos = 0;
-
- if (sb->len == 0)
- return (*this); // no character can be found in an empty string
-
- for (i=sb->len; (i > 0) && (pos == 0); i--) // Note: (i >= 0) is always TRUE for unsigned int,
- if (! ctx.contains(sb->str[i-1])) // so the range for i has been offset by +1
- pos = i;
-
- return (to(pos));
- }
-
- //_________________________________________________________________________________
-
- NString& NString::operator-= (const char c)
- {
- const char *fname = "operator-= (const char)";
- char *ctx = "x";
- unsigned long int newstart;
-
- ctx[0] = c;
- if ((newstart = strspn(sb->str, ctx)) == 0)
- return (*this);
-
- if (sb->refs > 1)
- {
- strbody *old_sb = sb;
-
- if (! GetNewSB(sb->len - newstart))
- OUT_OF_MEM(fname);
- if (sb->len == 0)
- return (*this);
- strcpy(sb->str, old_sb->str + newstart);
- }
- else
- {
- unsigned long int new_len = sb->len - newstart;
-
- memmove(sb->str, sb->str + newstart, new_len + 1);
- if (! ReallocStrBuf(new_len)) // shrink string buffer
- {
- sb->len = new_len; // necessary to keep the NString in a coherent state
- OUT_OF_MEM(fname); // this "Out of Memory" should never happen ...
- }
-
- sb->len = new_len;
- }
-
- return (*this);
- }
-
- //_________________________________________________________________________________
-
- NString& NString::operator-= (const Alphabet& ctx)
- {
- const char *fname = "operator-= (const Alphabet&)";
- unsigned long int newstart;
-
- for (newstart=0; (newstart < sb->len) && (ctx.contains(sb->str[newstart])); newstart++);
-
- if (newstart == 0)
- return (*this);
-
- if (sb->refs > 1)
- {
- strbody *old_sb = sb;
-
- if (! GetNewSB(sb->len - newstart))
- OUT_OF_MEM(fname);
- if (sb->len == 0)
- return (*this);
- strcpy(sb->str, old_sb->str + newstart);
- }
- else
- {
- unsigned long int new_len = sb->len - newstart;
-
- memmove(sb->str, sb->str + newstart, new_len + 1);
- if (! ReallocStrBuf(new_len)) // shrink string buffer
- {
- sb->len = new_len; // necessary to keep the NString in a coherent state
- OUT_OF_MEM(fname); // this "Out of Memory" should never happen ...
- }
-
- sb->len = new_len;
- }
-
- return (*this);
- }
-