--------------------------------------------------------------------------- Name: gtASCIIString::replace Description: Replaces a occurrences of a substring with another one. Arguments: oldSubString - The sub string to be replaced. replacementString - The sub string that will replace oldSubString. replaceAllOccurrences - true will replace all occurrences of oldSubString. false will just repl
| 1084 | // Date: 28/11/2005 |
| 1085 | // --------------------------------------------------------------------------- |
| 1086 | int gtASCIIString::replace(const gtASCIIString& oldSubString, const gtASCIIString& newSubString, |
| 1087 | bool replaceAllOccurrences) |
| 1088 | { |
| 1089 | int retVal = 0; |
| 1090 | |
| 1091 | // Verify that this string is not empty: |
| 1092 | if (!isEmpty()) |
| 1093 | { |
| 1094 | // Get the input sub strings lengths: |
| 1095 | int oldSubStrLen = oldSubString.length(); |
| 1096 | int newSubStrLen = newSubString.length(); |
| 1097 | |
| 1098 | // Will hold our current position in this string: |
| 1099 | int currentPos = 0; |
| 1100 | |
| 1101 | // While we didn't reach the end of the string: |
| 1102 | while (currentPos < length()) |
| 1103 | { |
| 1104 | if (_impl[currentPos] != '\0') |
| 1105 | { |
| 1106 | // Look for the old sub string from our current position: |
| 1107 | currentPos = (int)_impl.find(oldSubString.asCharArray(), currentPos); |
| 1108 | |
| 1109 | if ((currentPos == -1) || (currentPos > length())) |
| 1110 | { |
| 1111 | // The old sub string was not found - exit the loop: |
| 1112 | break; |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | // Replace this occurrence of the old string with the new one |
| 1117 | _impl.replace(currentPos, oldSubStrLen, newSubString.asCharArray(), newSubStrLen); |
| 1118 | |
| 1119 | // Update the current position: |
| 1120 | currentPos += newSubStrLen; |
| 1121 | |
| 1122 | // Increment the replace count |
| 1123 | retVal++; |
| 1124 | |
| 1125 | // If we were asked to replace only the first occurrence: |
| 1126 | if (!replaceAllOccurrences) |
| 1127 | { |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | return retVal; |
| 1136 | } |
| 1137 | |
| 1138 | // --------------------------------------------------------------------------- |
| 1139 | // Name: gtASCIIString::replace |
nothing calls this directly
no test coverage detected