gen_stri.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 812 行 · 第 1/3 页
C
812 行
this->str = NULL; // Invalidate pointer
this->validate(); // Check for sharing memory and reset
}
}
// strcpy -- String copy of a single character to an CoolGen_String object
// Input: CoolGen_String reference and a character
// Output: CoolGen_String object containing a character string
CoolGen_String& strcpy (CoolGen_String& s, char c) {
s.length = 1; // Character string length
s.validate(TRUE); // Ensure enough memory and not shared
*(s.str) = c; // Assign character
*(s.str+1) = END_OF_STRING; // END_OF_STRING terminator for string
return s; // Return CoolGen_String reference
}
// strcpy -- String copy of a char* to a String object
// Input: CoolGen_String reference and a char*
// Output: String object containing a character string
CoolGen_String& strcpy (CoolGen_String& s, const char *c) {
s.length = strlen (c); // Determine length of character string
s.validate(TRUE); // Ensure enough memory and not shared
strcpy(s.str, c); // Copy into string
return s; // Return CoolGen_String reference
}
// strcpy -- String copy of one String object to another. Note that the
// copy is delayed via the reference count mechanism until it
// is really needed.
// Input: Two CoolGen_String references
// Output: CoolGen_String object pointing to another CoolGen_String object
CoolGen_String& strcpy (CoolGen_String& s1, const CoolGen_String& s2) {
if (s1.p->ref_count > 1) // If sharing memory with other string
s1.p->ref_count--; // Decrement that reference count
else { // Else must be some memory to free
delete s1.p; // Free layout structure memory
if (s1.str != END_OF_STRING) // If pointing to a memory block
delete s1.str; // Free that up too
}
s1.length = s2.length; // Determine length of character string
s1.str = s2.str; // Point to same string memory block
s1.p = s2.p; // Point to same layout structure
s1.p->ref_count++; // Increment reference count
return s1; // Return CoolGen_String reference
}
// strcat -- Concatenate a single character to a CoolGen_String object
// Input: CoolGen_String reference and a character
// Output: CoolGen_String object concatenated with character
CoolGen_String& strcat (CoolGen_String& s, char c) {
s.length += 1; // Determine length of new string
s.validate(); // Ensure enough memory and not shared
s.str[s.length-1] = c; // Append new character
s.str[s.length] = END_OF_STRING; // END_OF_STRING terminator
return s; // Return String
}
// strcat -- Concatenate a String and a char*
// Input: CoolGen_String reference and a char*
// Output CoolGen_String object concatentated with character string
CoolGen_String& strcat (CoolGen_String& s, const char* c) {
long old_len = s.length; // Save length for efficiency hack
s.length = old_len + strlen (c); // Determine length of new string
s.validate(); // Ensure enough memory and not shared
strcpy (s.str+old_len, c); // Concatenate characters
return s; // Return String
}
// strncat -- Concatentate a String with "n" characters from char*
// Input: CoolGen_String reference, char*, number of characters
// Output: CoolGen_String object concatenedate with "n" characters
CoolGen_String& strncat (CoolGen_String& s, const char* c, int n) {
#if ERROR_CHECKING
if (n < 0) { // If invalid length
printf ("CoolGen_String::strncat(): Negative length %d.\n", n);
exit (1);
}
#endif
long old_len = s.length; // Save length for efficiency hack
s.length = old_len + n; // Determine length of new string
s.validate(); // Ensure enough memory and not shared
strncpy (s.str+old_len, c, size_t(n)); // Concatenate characters
return s; // Return String
}
// strcat -- Concatenate two CoolGen_String objects
// Input: Two CoolGen_String references
// Output: CoolGen_String object concatenated with CoolGen_String object
CoolGen_String& strcat (CoolGen_String& s1, const CoolGen_String& s2) {
long old_len = s1.length; // Save length for efficiency hack
s1.length = old_len + s2.length; // Determine length of new string
s1.validate(); // Ensure enough memory and not shared
strcpy (s1.str+old_len, s2.str); // Copy remaining characters
return s1; // Return String
}
// strncat -- Concatentate a String with "n" characters from a String
// Input: Two CoolGen_String references and number of characters
// Output: CoolGen_String object concatenedate with "n" characters
CoolGen_String& strncat (CoolGen_String& s1, const CoolGen_String& s2, int n) {
#if ERROR_CHECKING
if (n < 0) { // If invalid length
printf ("CoolGen_String::strncat(): Negative length %d.\n", n);
exit (1);
}
#endif
long old_len = s1.length; // Save length for efficiency hack
s1.length = old_len + n; // Determine length of new string
s1.validate(); // Ensure enough memory and not shared
strncpy (s1.str+old_len, s2.str, size_t(n)); // Copy remaining characters
return s1; // Return String
}
// compile -- Compile a regular expression ready for pattern matching. If no
// regexp object in string, create one
// Input: Regular expression to compile
// Output: None
void CoolGen_String::compile (const char* s) {
if (this->rgexp == NULL) // If no regexp object
this->rgexp = new CoolRegexp; // Allocate storage for regexp
this->rgexp->compile (s); // Compile regular expression
this->rgexp_index = 0; // Reset search index position
}
// find -- Search the string for the previously compiled regular expression
// Input: None
// Output: Boolean TRUE/FALSE
Boolean CoolGen_String::find () {
#if ERROR_CHECKING
if (this->rgexp == NULL || !this->rgexp->is_valid()) // Valid regexp?
this->find_error (); // Raise exception
#endif
this->rgexp_index += this->rgexp->end (); // Use index from last search
if (this->rgexp_index >= this->length) // If at end of search
return FALSE; // Indicate failure
Boolean result = this->rgexp->find (this->str + this->rgexp_index);
return result; // Return match result
}
// reverse -- Reverse the order of the characters in CoolGen_String object
// Input: CoolGen_String object
// Output: CoolGen_String object with character order reverse
void CoolGen_String::reverse () {
char c;
for (long i = 0, j = this->length-1; // Counting from front and rear
i < this->length / 2; i++, j--) { // until we reach the middle
c = this->str[i]; // Save front character
this->str[i] = this->str[j]; // Switch with rear character
this->str[j] = c; // Copy new rear character
}
}
// resize -- Adjust the memory size of a string to accomodate some size
// Input: CoolGen_String object
// Output: None
void CoolGen_String::resize (long sz) {
#if ERROR_CHECKING
if (sz < 0) { // If invalid resize
printf ("CoolGen_String::resize(): Negative resize %d.\n", sz);
exit (1);
}
#endif
int shared_memory = FALSE; // If TRUE, memory shared
char* temp = this->str; // Save pointer to existing string
if (this->p->ref_count > 1) { // If sharing memory with other String
this->update_ref_count(); // Adjust and update
shared_memory = TRUE; // Set flag for later use
}
this->str = new char[sz+1]; // Allocate memory for desired size
this->p->start = str; // Pointer to start of string
this->p->size = sz+1; // Save size of allocated memory
if (temp != NULL) // If original string had data
strcpy ((char *)this->str, temp); // Copy original string back
if (shared_memory == FALSE) // If did not share memory
delete temp; // Deallocate memory
}
// operator<< -- Overload output operator for CoolGen_String objects
// Input: CoolGen_String object
// Output: Formatted output and stream descriptor
ostream& operator<< (ostream& os, const CoolGen_String& s) {
return os << s.str; // Output char* and newline
}
// strtol -- Returns as a long integer the value represented by the
// string pointer to by s, scanning upto the first character
// that is inconsistent with the base. Leading white space is
// ignored.
// Input: Reference to CoolGen_String object
// Radix of number
// Output: Long integer representing value contained in the String
long strtol (const CoolGen_String& s, char** ptr, int radix) {
return (strtol ((char *)s.str, ptr, radix));
}
// atol -- Equivalent to: strtol (str, (char**)END_OF_STRING, 10)
// Input: Reference to CoolGen_String object
// Output: Long integer representing value contained in the String
long atol (const CoolGen_String& s) {
return (strtol ((char *)s.str, (char **) END_OF_STRING, int(10)));
}
// atoi -- Equivalent to: (int)strtol (str, (char**)END_OF_STRING, 10)
// Input: Reference to CoolGen_String object
// Output: Integer representing value contained in the String
int atoi (const CoolGen_String& s) {
return int(strtol ((char *)s.str, (char **) END_OF_STRING, int(10)));
}
// strtod -- Returns as a double-precision floating-point number the
// value represented by a CoolGen_String object. Characters are
// scanned upto the first unrecognized character.
// Input: Reference to CoolGen_String object
// Output: Double representing value contained in String
double strtod (const CoolGen_String& s, char** ptr) {
return (strtod ((char *)s.str, ptr));
}
// trim -- Removes any occurrence of the character(s) in "c" from "s"
// Input: CoolGen_String reference, character string
// Output: Modified String "s" object
CoolGen_String& trim (CoolGen_String& sr, const char* rem) {
sr.validate(); // Ensure not shared
char* s = sr.str;
char* result = sr.str;
long len = 0;
register char c;
while ((c=*s++) != END_OF_STRING) {
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
*result++ = c, len++;
}
*result = END_OF_STRING; // NULL terminate string
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?