void CombinedSpellSuggestions::replaceToSuggested(ISpellCheckable *item) { if (anyReplacementSelected()) { const QString &word = getWord(); const QString &replacement = getReplacement(); this->replaceToSuggested(item, word, replacement); } }
string &Replacement::apply(string &str) const { switch(getType()) { case REPLACE_FIRST: { size_t idx = str.find(getOriginal()); if (idx == string::npos) return str; size_t len = getOriginal().length(); str.replace(idx, len, getReplacement()); return str; } case REPLACE_ALL: { size_t offset = 0; string copy = str; while (true) { size_t idx = copy.find(getOriginal()); if (idx == string::npos) return str; size_t len = getOriginal().length(); str.replace(offset + idx, len, getReplacement()); offset += idx + len; copy = str.c_str() + offset; } } case REPLACE_END: { size_t offset = str.length() - getOriginal().length(); if (strcmp(str.c_str() + offset, getOriginal().c_str()) != 0) { return str; } str.replace(offset, getOriginal().length(), getReplacement()); return str; } default: return str; } }
// hExpand() returns a copy of the string OLDLINE in which any substring of // // the command line of the form !! or !N or !-N or !?STRING? as follows: // // // // !! -> last remembered command // // // // !N -> N-th command read (N is any sequence of digits) // // // // !-N -> N-th last remembered command (N is any sequence of digits) // // // // !?STRING? -> most recent remembered command with a token that contains // // STRING (= a nonempty sequence of characters that does not // // contain whitespace or ! or ?) // // // // The replacement string consists of the specified sequence of tokens // // separated by single blanks. // // // // If the notation above is followed by a *, then the leading token in the // // command is omitted. If it is followed by a ^, then only the first token is // // used. If it is followed by a :M (where M is any sequence of digits), then // // only the M-th token is used. If it is followed by a $, then only the last // // token is used. // // // // The search for substrings to expand proceeds from left to right and // // continues after each replacement string. If the requested substitution // // is impossible, then the substring is deleted, but the search continues. // // Storage for the new line is allocated by malloc()/realloc(). The string // // OLDLINE is not modified. // // // // hExpand() sets STATUS to 1 if substitutions were made and all succeeded, to // // 0 if no substitutions were requested, and to -1 if some substitution failed. // // char *hExpand (const char *oldLine, int *status) { char *expanded = malloc(1); expanded[0] = '\0'; int stringLength = strlen(oldLine); int statusTracker = 0; int iPointer[1]; for (int i = 0; i < stringLength - 1; i++) { if (oldLine[i] == '!' && i + 1 < stringLength - 1){ int replacementStatus[1]; iPointer[0] = i; replacementStatus[0] = 0; iPointer[0] ++; char *replacement = getReplacement (iPointer, oldLine, replacementStatus); if (replacementStatus[0] == -1) { char *letterToAdd = malloc(2); letterToAdd[0] = oldLine[i]; letterToAdd[1] = '\0'; expanded = combine (expanded, letterToAdd); } else if (!replacement) { statusTracker = -1; i = iPointer[0]; } else if (replacementStatus[0] != -1) { if (statusTracker != -1){ statusTracker = 1; } expanded = combine (expanded, replacement); i = iPointer[0]; } } else { char *letterToAdd = malloc(2); letterToAdd[0] = oldLine[i]; letterToAdd[1] = '\0'; expanded = combine (expanded, letterToAdd); } } status[0] = statusTracker; char *endString = malloc(2); endString[0] = '\n'; endString[1] = '\0'; expanded = combine(expanded, endString); return expanded; }