mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-31 23:28:45 +00:00
Factor out count_preceding_backslashes
Now that we have multiple clients of count_preceding_backslashes, factor it out from fish_indent into wcstringutil.h, and then use the shared implementation.
This commit is contained in:
parent
cff5aa9130
commit
409ed7d6d0
4 changed files with 19 additions and 33 deletions
|
@ -46,6 +46,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
#include "parse_constants.h"
|
#include "parse_constants.h"
|
||||||
#include "parse_util.h"
|
#include "parse_util.h"
|
||||||
#include "print_help.h"
|
#include "print_help.h"
|
||||||
|
#include "wcstringutil.h"
|
||||||
#include "wutil.h" // IWYU pragma: keep
|
#include "wutil.h" // IWYU pragma: keep
|
||||||
|
|
||||||
// The number of spaces per indent isn't supposed to be configurable.
|
// The number of spaces per indent isn't supposed to be configurable.
|
||||||
|
@ -87,17 +88,6 @@ namespace {
|
||||||
template <bool B, typename T = void>
|
template <bool B, typename T = void>
|
||||||
using enable_if_t = typename std::enable_if<B, T>::type;
|
using enable_if_t = typename std::enable_if<B, T>::type;
|
||||||
|
|
||||||
/// \return the number of escaping backslashes before a character.
|
|
||||||
/// \p idx may be "one past the end."
|
|
||||||
size_t count_preceding_backslashes(const wcstring &text, size_t idx) {
|
|
||||||
assert(idx <= text.size() && "Out of bounds");
|
|
||||||
size_t backslashes = 0;
|
|
||||||
while (backslashes < idx && text.at(idx - backslashes - 1) == L'\\') {
|
|
||||||
backslashes++;
|
|
||||||
}
|
|
||||||
return backslashes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \return whether a character at a given index is escaped.
|
/// \return whether a character at a given index is escaped.
|
||||||
/// A character is escaped if it has an odd number of backslashes.
|
/// A character is escaped if it has an odd number of backslashes.
|
||||||
bool char_is_escaped(const wcstring &text, size_t idx) {
|
bool char_is_escaped(const wcstring &text, size_t idx) {
|
||||||
|
|
|
@ -3227,28 +3227,11 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat
|
||||||
|
|
||||||
// Historical behavior is to trim trailing spaces.
|
// Historical behavior is to trim trailing spaces.
|
||||||
// However, escaped spaces ('\ ') should not be trimmed (#7661)
|
// However, escaped spaces ('\ ') should not be trimmed (#7661)
|
||||||
while (!text.empty() && text.back() == L' ') {
|
// This can be done by counting pre-trailing '\'
|
||||||
// This can be done by counting pre-trailing '\'
|
// If there's an odd number, this must be an escaped space.
|
||||||
// If there's an odd number, this must be an excaped space
|
while (!text.empty() && text.back() == L' ' &&
|
||||||
|
count_preceding_backslashes(text, text.size() - 1) % 2 == 0) {
|
||||||
// first check if the trailing space can be escaped at all
|
text.pop_back();
|
||||||
// (making the common case fast)
|
|
||||||
if(text.length() == 1 || text[text.length()-2] != L'\\') {
|
|
||||||
text.pop_back();
|
|
||||||
} else {
|
|
||||||
// We've found one backslash so far, count the rest
|
|
||||||
size_t num_pretrailing_backslashes = 1;
|
|
||||||
for(size_t i_offset=text.length()-2;
|
|
||||||
i_offset > 0 && text[i_offset-1] == L'\\'; --i_offset)
|
|
||||||
num_pretrailing_backslashes++;
|
|
||||||
if(num_pretrailing_backslashes%2 == 0) {
|
|
||||||
// even; this space is not escaped and should be removed
|
|
||||||
text.pop_back();
|
|
||||||
}
|
|
||||||
// Because the above accounts for the final character,
|
|
||||||
// we're done here. Break so there's not an infinite loop
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (history && !conf.in_silent_mode) {
|
if (history && !conf.in_silent_mode) {
|
||||||
|
|
|
@ -74,6 +74,15 @@ wcstring wcstolower(wcstring input) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t count_preceding_backslashes(const wcstring &text, size_t idx) {
|
||||||
|
assert(idx <= text.size() && "Out of bounds");
|
||||||
|
size_t backslashes = 0;
|
||||||
|
while (backslashes < idx && text.at(idx - backslashes - 1) == L'\\') {
|
||||||
|
backslashes++;
|
||||||
|
}
|
||||||
|
return backslashes;
|
||||||
|
}
|
||||||
|
|
||||||
bool string_prefixes_string(const wchar_t *proposed_prefix, const wcstring &value) {
|
bool string_prefixes_string(const wchar_t *proposed_prefix, const wcstring &value) {
|
||||||
return string_prefixes_string(proposed_prefix, value.c_str());
|
return string_prefixes_string(proposed_prefix, value.c_str());
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,6 +220,10 @@ wcstring trim(wcstring input, const wchar_t *any_of);
|
||||||
/// Converts a string to lowercase.
|
/// Converts a string to lowercase.
|
||||||
wcstring wcstolower(wcstring input);
|
wcstring wcstolower(wcstring input);
|
||||||
|
|
||||||
|
/// \return the number of escaping backslashes before a character.
|
||||||
|
/// \p idx may be "one past the end."
|
||||||
|
size_t count_preceding_backslashes(const wcstring &text, size_t idx);
|
||||||
|
|
||||||
// Out-of-line helper for wcs2string_callback.
|
// Out-of-line helper for wcs2string_callback.
|
||||||
void wcs2string_bad_char(wchar_t);
|
void wcs2string_bad_char(wchar_t);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue