📄 sstr_03.cc
字号:
//// return: a boolean value indicating status//// this method converts the object into a SysChar//boolean SysString::get(SysChar& val_a) const { // if length is 1 then assign the element to output SysChar // if (length() == 1) { return val_a.assign(value_d[0]); } // exit ungracefully // return false;}// method: memSize//// arguments: none//// return: the size of the class in bytes//// return the size of the class in bytes//long SysString::memSize() const { // start with the size of the pointer // long s = sizeof(value_d); // add the dynamically allocated string memory // s += sizeof(unichar) * (capacity_d + 1); // add the capacity // s += sizeof(capacity_d); // return the size // return s;}// method: substr//// arguments:// SysString& str: (output) the substring// long offset: (input) where to start copying from// long num_elements: (input) number of characters to copy//// return: a boolean value indicating status//// return a substring of the current object//boolean SysString::substr(SysString& str_a, long offset_a, long num_elements_a) const { // check the arguments // if (&str_a == this) { return Error::handle(name(), L"substr", Error::MEM, __FILE__, __LINE__); } // offset can't be negative // if (offset_a < 0) { offset_a = 0; } // clear the string and exit ungracefully if offset is greater than // the length of this or number of characters to copy is 0 // if ((offset_a > length()) || (num_elements_a == 0)) { str_a.clear(Integral::RESET); return false; } // number of characters to copy can't be negative // if (num_elements_a < 0) { num_elements_a = length() - offset_a; } // allocate a buffer and assign the values // unichar buffer[num_elements_a + 1]; isip_wcsncpy(buffer, &value_d[offset_a], num_elements_a); buffer[num_elements_a] = (unichar)NULL; str_a.assign(buffer); // exit gracefully // return true;}// method: insert//// arguments:// const unichar* str: (output) the substring// long offset: (input) where to start copying into//// return: a boolean value indicating status//// insert string str into current string//boolean SysString::insert(const unichar* str_a, long offset_a) { // declare local variable // SysString temp; // assign the input buffer to a string // temp.assign(str_a); // call the master function // return insert(temp, offset_a);} // method: insert//// arguments:// const SysString& str: (output) the substring// long offset: (input) where to start copying into//// return: a boolean value indicating status//// insert string str into current string//boolean SysString::insert(const SysString& str_a, long offset_a) { // check if the offset is greater that the length // if (offset_a > length()) { return concat(str_a); } // make sure the argument isn't the object // if (&str_a == this) { return Error::handle(name(), L"insert", Error::MEM, __FILE__, __LINE__); } // if offset is negative then set it to zero // if (offset_a <= 0) { offset_a = 0; // make a new string which copies the input string (preserves argument) // SysString temp(str_a); // concatenate this string to temporary // temp.concat(*this); // swap pointers // return swap(temp); } // make sure we have a large enough buffer // if ((length() + str_a.length()) > capacity_d) { if (!growMem(length() + str_a.length())) { return Error::handle(name(), L"insert", Error::MEM, __FILE__, __LINE__); } } // get the length of the input and this string // long len0 = length(); long len1 = str_a.length(); // now insert // for (long i = len0 + len1; i >= offset_a + len1; i--) { value_d[i] = value_d[i - len1]; } for (long i = offset_a + len1 - 1; i >= offset_a; i--) { value_d[i] = str_a.value_d[i - offset_a]; } // exit gracefully // return true;}// method: replace//// arguments:// const unichar* str: (output) the substring// long offset: (input) where to start copying into//// return: a boolean value indicating status//// insert string str into current string//boolean SysString::replace(const unichar* str_a, long offset_a) { // declare local variables // static SysString temp; // assign the unichar* to a string // temp.assign(str_a); // call the master function // return replace(temp, offset_a);}// method: replace//// arguments:// const SysString& str: (output) the substring// long offset: (input) where to start copying into//// return: a boolean value indicating status//// insert string str into current string//boolean SysString::replace(const SysString& str_a, long offset_a) { // check the length of string // if (str_a.length() > 0) { // concat str_a if offset is greater than the length of the // current string // if (offset_a > length()) { return concat(str_a); } // if the new string will be longer than the old string, we don't // have to worry about trailing characters // if (str_a.length() >= (length() - offset_a)) { // cut the current value at the insertion point // value_d[offset_a] = (unichar)NULL; // concatenate the new string // concat(str_a); } // the old string is plenty big enough, just copy new characters // in by hand // else { long max = str_a.length(); for (long i = 0; i < max; i++) { value_d[i + offset_a] = str_a.value_d[i]; } } } // exit gracefully // return true;}// method: replace//// arguments:// const SysString& pattern: (input) the pattern to search for// const SysString& new_stuff: (input) the replacement string//// return: a boolean value indicating status//// search for a pattern string and replace it with the new_stuff//boolean SysString::replace(const SysString& pattern_a, const SysString& new_stuff_a) { // find the first instance of the pattern // long index = firstStr(pattern_a); // if no instance is found, return false // if (index == Integral::NO_POS) { return false; } deleteRange(index, pattern_a.length()); insert(new_stuff_a, index); // exit gracefully // return true;} // method: replaceAll//// arguments:// const SysString& pattern: (input) the pattern to search for// const SysString& new_stuff: (input) the replacement string//// return: a boolean value indicating status//// search for a pattern string and replace it with the new_stuff//boolean SysString::replaceAll(const SysString& pattern_a, const SysString& new_stuff_a) { // local variables // long count = 0; long len = 0; long index = 0; // get the length of the new string // len = new_stuff_a.length(); // replace all instances of the pattern // while ((index = firstStr(pattern_a, index)) != Integral::NO_POS) { deleteRange(index, pattern_a.length()); insert(new_stuff_a, index); index += len; } // exit gracefully // return (count > 0);} // method: deleteRange//// arguments:// long offset: (input) where to start deleting// long num_elements: (input) number of elements to delete//// return: a boolean value indicating status//// delete a range of characters from the current string//boolean SysString::deleteRange(long offset_a, long num_elements_a) { // check the arguments // if ((offset_a < 0) || (offset_a >= length()) || ((offset_a + num_elements_a) > length())) { return Error::handle(name(), L"deleteRange", Error::ARG, __FILE__, __LINE__); } // check for number of elements // if (num_elements_a < 0) { num_elements_a = length() - offset_a; } // see if we need to worry about trailing characters, or if we can // just get away with deleting. // if ((offset_a + num_elements_a) >= length()) { value_d[offset_a] = (unichar)NULL; return true; } // we do need to worry about trailing characters // value_d[offset_a] = (unichar)NULL; // concatenate the trailing characters // concat(&value_d[offset_a + num_elements_a]); // exit gracefully // return true;}// method: isip_wcsstr//// arguments:// const unichar* ws1: (input) wc string to search// const unichar* ws2: (input) wc substring to find//// return: unichar* ptr to first occurrence of a ws2 in ws1//// wrap system wcsstr method//unichar* SysString::isip_wcsstr(const unichar* ws1_a, const unichar* ws2_a) { // which system function has to do with which operating system //#if ISIP_WCHAR_MODE == ISIP_WCHAR_SOLARIS // call the solaris 2.6 system function // return ::wcswcs(ws1_a, ws2_a);#else // call the more standard system function // return ::wcsstr(ws1_a, ws2_a);#endif}// method: isip_wcstok//// arguments:// unichar* ws: (input) wc string to tokenize// const unichar* delim: (input) delimiter character// unichar** ptr: (input) temporary workspace//// return: unichar* ptr to first occurrence of a needle in the haystack//// wrap system wcschr method//unichar* SysString::isip_wcstok(unichar* ws_a, const unichar* delim_a, unichar** ptr_a) { // which system function has to do with which operating system //#if ISIP_WCHAR_MODE == ISIP_WCHAR_SOLARIS // solaris 2.6 needs an older version of wcstok // return ::wcstok(ws_a, delim_a);#else // any other supporting operating system uses the new definition // return ::wcstok(ws_a, delim_a, ptr_a);#endif }// method: isip_fputws//// arguments:// const unichar* s: (input) string to write// FILE* stream: (input) stream to write to//// return: a non-negative number//// wrap system fputws function//long SysString::isip_fputws(const unichar* s_a, FILE* stream_a) { // call the system function //#if ISIP_WCHAR_MODE != ISIP_WCHAR_NONE return ::fputws(s_a, stream_a);#else // build a string from the input (without copying) // SysString temp_string; temp_string.value_d = (unichar*)s_a; temp_string.capacity_d = isip_wcslen(s_a); // write this string // int ret = ::fputs((char*)(byte*)temp_string, stream_a); // make sure we don't delete the users buffer // temp_string.value_d = (unichar*)NULL; temp_string.capacity_d = 0; temp_string.allocateMem(); // return the system return value // return ret;#endif}// method: isip_fgetws//// arguments:// unichar* s: (output) string read// int n: (input) maximum number of characters to read// FILE* stream: (input) stream to read from//// return: pointer to read string//// wrap system fgetws function//unichar* SysString::isip_fgetws(unichar* s_a, int n_a, FILE* stream_a) { // call the system function //#if ISIP_WCHAR_MODE != ISIP_WCHAR_NONE return ::fgetws(s_a, n_a, stream_a);#else // read in the characters // char* input_buffer = new char[n_a + 1]; MemoryManager::memset(input_buffer, 0, n_a + 1); // call the system fgets function, check return value // if (::fgets(input_buffer, n_a, stream_a) == (char*)NULL) { return (unichar*)NULL; } // declare a SysChar to hold characters // SysChar c; // loop through each character, converting from char to SysChar // for (long i = 0; i < n_a; i++) { c.assign((byte)input_buffer[i]); c.get(s_a[i]); } delete [] input_buffer; // return the pointer // return s_a;#endif}// method: isip_fputwc//// arguments:// wint_t c: (input) character to write// FILE* stream: (input) stream to write to//// return: a non-negative number//// wrap system fputws function//wint_t SysString::isip_fputwc(wint_t c_a, FILE* stream_a) { // call the system function //#if ISIP_WCHAR_MODE != ISIP_WCHAR_NONE return ::fputwc(c_a, stream_a);#else // build a SysChar from the input // SysChar c((unichar)c_a); byte output; c.get(output); // write this string // int ret = ::fputc(output, stream_a); // return the system return value // return (wint_t)ret;#endif}// method: isip_fgetwc//// arguments:// FILE* stream: (input) stream to read from//// return: pointer to read string//// wrap system fgetws function//wint_t SysString::isip_fgetwc(FILE* stream_a) { // call the system function //#if ISIP_WCHAR_MODE != ISIP_WCHAR_NONE return ::fgetwc(stream_a);#else // read in the characters // int input; input = ::fgetc(stream_a); // check for end of file and do the appropriate conversion // if (input == (int)EOF) { return (wint_t)WEOF; } // build a SysChar from the input // SysChar c((byte)input); // return the character // return (unichar)c;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -