gen_stri.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 812 行 · 第 1/3 页

C
812
字号
  sr.length = len;
  return sr;                                    // Return string
}


// left_trim -- Removes any occurrence of the character(s) in "c" from
//              "s" that appear as a prefix to the string. The first
//              non-matching character encountered terminates the remove
//              operation and the rest of the string is copied intact.
// Input:       CoolGen_String reference, character string
// Output:      Modified String "s" object

CoolGen_String& left_trim (CoolGen_String& sr, const char* rem) {
  sr.validate();                        // Ensure not shared
  char* result = sr.str;
  char* s = sr.str;
  long len = 0;
  register char c;
  for (; (c=*s) != END_OF_STRING; s++) {
    register const char* r = rem;
    register char t;
    while ((t=*r++) != END_OF_STRING && t != c); // Scan for match
    if (t == END_OF_STRING)                      // If no match found
      break;
  }
  if (s != result)                                // when characters trimed
    while ((*result++ = *s++) != END_OF_STRING) len++; // shift string down
  sr.length = len;
  return sr;                            // Return string reference
}


// right_trim -- Removes any occurrence of the character(s) in "c" from
//               "s" that appear as a suffix to the string. The first
//               non-matching character encountered terminates the remove
//               operation and the rest of the string is copied intact.
// Input:        CoolGen_String reference, character string
// Output:       Modified String "s" object

CoolGen_String& right_trim (CoolGen_String& sr, const char* rem) {      
  sr.validate();                        // Ensure not shared
  char* str = sr.str;
  char* s = str + strlen(str) - 1;              // last character of string
  for (; s >= str; s--) {
    register const char* r = rem;
    register char t;
    register char c = *s;
    while ((t=*r++) != END_OF_STRING && t != c); // Scan for match
    if (t == END_OF_STRING)                      // If no match found
      break;
  }
  *(s+1) = END_OF_STRING;
  sr.length = s - str;
  return sr;                            // Return String reference
}


// upcase -- Convert all alphabetical characters to uppercase
// Input:    CoolGen_String reference
// Output:   Updated string

CoolGen_String& upcase (CoolGen_String& s) {    // Convert entire string to upper case
  s.validate();                         // Ensure not shared
  c_upcase(s.str);
  return s;                             // Return reference to modified string
}


// downcase -- Convert all alphabetical characters to lowercase
// Input:      CoolGen_String reference
// Output:     Updated string

CoolGen_String& downcase (CoolGen_String& s) {  // Convert entire string to lower case
  s.validate();                 // Ensure not shared
  c_downcase(s.str);
  return s;                     // Return reference to modified string
}

// capitalize -- Capitalize all words in a String. A word is define as
//               a sequence of characters separated by white space
// Input:        CoolGen_String reference
// Output:       Updated string

CoolGen_String& capitalize (CoolGen_String& s) {        
  s.validate();                 // Ensure not shared
  c_capitalize(s.str);
  return s;                     // Return reference to modified string
}


// insert -- Insert a char* at the position specified by start.
// Input  -- A char* to be inserted and a long index.          
// Output -- A Boolean, true if insertion took place, false if error.

Boolean CoolGen_String::insert (const char* ins, long start) {
  if (start<0 || start>this->length) return(FALSE);  // Boundary check
  long len=strlen(ins);                 // length of char*
  this->length += len;                  // Determine length of new string
  this->validate();                     // Ensure not shared and enough memory
  register char* ptr = this->str;
  register char* st = ptr + start;
  register char* end = ptr + this->length;
  for (ptr = end - len; ptr >= st;)             // Make space for ins
    *end-- = *ptr--;
  for (end = st+len; st < end; *st++ = *ins++); // Insert ins into that
  return(TRUE);                         // Insert worked
}


// remove -- Removes everything between the start and end indexes of string
//           The character at start is removed and all characters up to but 
//           not including the character at end.
// Input  -- A start and end index into the string.
// Output -- A Boolean, true if remove worked, false if error.

Boolean CoolGen_String::remove (long start, long end) {
#if ERROR_CHECKING
  if (start < 0 || start > this->length || end < 0
      || end > this->length || end <= start) {
    printf ("CoolGen_String::remove(): Start %d and/or end %d index invalid.\n",
            start, end);
    exit (1);
  }
#endif
  this->validate();                          // Ensure not shared
  this->length -= (end-start);               // New (shorter) length
  for(; this->str[start]=this->str[end]; start++) end++; // Remove everything
  return(TRUE);                              // Remove worked
}


// replace -- Removes everything between start and end as in remove(), and
//            inserts the char* argument into the place of what was removed.
// Input   -- A start and end for remove and a char*, c, to insert.
// Output  -- A Boolean, true if replace worked, false if error.

Boolean CoolGen_String::replace (const char* c, long start, long end) {
#if ERROR_CHECKING
  if (start < 0 || start > this->length || end < 0            // Boundary check
      || end > this->length || end <= start) {
    printf ("CoolGen_String::replace(): Start %d and/or end %d index invalid.\n",
            start, end);
    exit (1);
  }
#endif
  long len=strlen(c);                   // length of c
  long delta = len - end + start;       // find overall change in length
  this->length += delta;                // set new length
  this->validate();                     // Ensure not shared and enough memory
  long ind;
  if (delta > 0) {                                // If replacement is bigger
    for (ind=this->length; start+len<=ind; ind--) // than chacters replaced
      this->str[ind] = this->str[ind-delta];
  }
  else if (delta < 0) {                 // Replacement is shorter
    char* st = this->str + start + len;
    char* ptr = this->str + end;
    char* end = this->str + this->length;
    while (st<=end) *st++ = *ptr++;
  }
  { char* ptr = this->str+start;
    char ch;
    while ((ch = *c++) != END_OF_STRING) // replace characters
      *ptr++ = ch;
  }
  return(TRUE);
}


// yank   -- Removes everything between start and end, copies it into a 
//           a string and returns that string.
// Input  -- A reference to a CoolGen_String. A start and end for the remove.
// Output -- none.  the CoolGen_String passed in gets set.

void CoolGen_String::yank (CoolGen_String& s, long start, long end) {
#if ERROR_CHECKING
  if (start < 0 || start > this->length || end < 0      // Boundary check
      || end > this->length || end <= start) {
    printf ("CoolGen_String::yank(): Start %d and/or end %d index invalid.\n",
            start,end);
    exit (1);
  }
#endif
  this->validate();                     // Ensure not shared
  long len = end - start;
  strncpy(s, this->str+start, len);     // Copy stuff to yank into s
  this->length -= len;                  // set new length
  for(; this->str[start]=this->str[end]; start++) end++;  // Do the remove
}


// sub_string -- Returns a new string initialized to what is between the
//               start and end indexes.
// Input      -- A CoolGen_String reference.  Two longs, a start and an end
// Output     -- none.  Sets the given CoolGen_String.

void CoolGen_String::sub_string (CoolGen_String& s, long start, long end) {
#if ERROR_CHECKING
  if (start < 0 || start > this->length || end < 0      // Boundary check
      || end > this->length || end <= start) {
    printf ("CoolGen_String::sub_string(): Start %d and/or end %d index invalid.\n",
            start, end);
    exit (1);
  }
#endif
  strncpy(s, this->str+start, end - start);     // Copy substring into s
}


// strncpy -- Returns s, with the first length characters of source copied
//            into it.  The old value of s is lost.
// Input   -- A reference to a CoolGen_String s, a char* source, and a long length.
// Output  -- The modified CoolGen_String s.

CoolGen_String& strncpy(CoolGen_String& s, const char* source, long length) {
#if ERROR_CHECKING  
  if (length < 0) {                     // Boundary check
    printf ("CoolGen_String::strncpy(): Negative length %d.\n", length);
    exit (1);
  }
#endif
  s.length = length;                    // Set new string length
  s.validate(TRUE);                     // Ensure not shared and enough memory
  char* p = s.str;
  while (length-- > 0)                  // Copy source into string
    *p++ = *source++;
  *p = END_OF_STRING;                   // set the end byte
  return s;
}


// bracket_error -- Raise exception for operator[]
// Input:           Index
// Output:          None

void CoolGen_String::bracket_error (long n) {
  printf ("CoolGen_String::operator[](): Index %d out of range.\n", n);
  exit (1);
}


// ratio_error -- Raise exception for set_growth_ratio
// Input:         Ratio
// Output:        None

void CoolGen_String::ratio_error (float r) {
  printf ("CoolGen_String::set_growth_ratio(): Negative growth ratio %f.\n", r);
  exit (1);
}


// size_error -- Raise exception for set_alloc_size
// Input:        Growth size
// Output:       None

void CoolGen_String::size_error (int n) {
  printf ("CoolGen_String::set_alloc_size(): Negative growth size %d.\n", n);
  exit (1);
}


// find_error -- Raise exception for find()
// Input:        None
// Output:       None

void CoolGen_String::find_error () {
  printf ("CoolGen_String::find(): Invalid regular expression.\n");
  exit (1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?