From 1834a67b87b7a8f6a9108721910e8be192471211 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 19 May 2016 21:45:23 +0100 Subject: [PATCH] Go back to libc locale and use c_regex_traits Unfortunately, cygwin does not support c++ locales. --- src/highlighters.cc | 2 +- src/main.cc | 3 +-- src/ranked_match.cc | 7 ++----- src/regex.cc | 2 +- src/regex.hh | 17 +++++++++++------ src/unicode.hh | 6 +++--- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/highlighters.cc b/src/highlighters.cc index bcab234e..fd1a4567 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -884,7 +884,7 @@ void expand_unprintable(const Context& context, HighlightFlags flags, DisplayBuf { auto coord = it.coord(); Codepoint cp = utf8::read_codepoint(it, end); - if (cp != '\n' and not std::isprint((wchar_t)cp, std::locale{})) + if (cp != '\n' and not iswprint((wchar_t)cp)) { if (coord != atom_it->begin()) atom_it = ++line.split(atom_it, coord); diff --git a/src/main.cc b/src/main.cc index fb4c16d1..4db4f883 100644 --- a/src/main.cc +++ b/src/main.cc @@ -707,8 +707,7 @@ int run_pipe(StringView session) int main(int argc, char* argv[]) { - try { std::locale::global(std::locale("")); } - catch (std::runtime_error&) { setlocale(LC_ALL, ""); } + setlocale(LC_ALL, ""); set_signal_handler(SIGSEGV, signal_handler); set_signal_handler(SIGFPE, signal_handler); diff --git a/src/ranked_match.cc b/src/ranked_match.cc index 77025219..a6145d62 100644 --- a/src/ranked_match.cc +++ b/src/ranked_match.cc @@ -34,7 +34,6 @@ using Utf8It = utf8::iterator; static int count_word_boundaries_match(StringView candidate, StringView query) { - std::locale locale; int count = 0; Utf8It query_it{query.begin(), query}; Codepoint prev = 0; @@ -42,10 +41,8 @@ static int count_word_boundaries_match(StringView candidate, StringView query) { const Codepoint c = *it; const bool is_word_boundary = prev == 0 or - (!std::isalnum((wchar_t)prev, locale) and - std::isalnum((wchar_t)c, locale)) or - (std::islower((wchar_t)prev, locale) and - std::isupper((wchar_t)c, locale)); + (!iswalnum((wchar_t)prev) and iswalnum((wchar_t)c)) or + (iswlower((wchar_t)prev) and iswupper((wchar_t)c)); prev = c; if (not is_word_boundary) diff --git a/src/regex.cc b/src/regex.cc index d70d8dd6..4482cb59 100644 --- a/src/regex.cc +++ b/src/regex.cc @@ -8,7 +8,7 @@ namespace Kakoune using Utf8It = RegexUtf8It; Regex::Regex(StringView re, flag_type flags) try - : boost::wregex{Utf8It{re.begin(), re}, Utf8It{re.end(), re}}, m_str(re.str()) + : RegexBase{Utf8It{re.begin(), re}, Utf8It{re.end(), re}}, m_str(re.str()) {} catch (std::runtime_error& err) { throw regex_error(err.what()); } String option_to_string(const Regex& re) diff --git a/src/regex.hh b/src/regex.hh index 9e966c33..f2457f4d 100644 --- a/src/regex.hh +++ b/src/regex.hh @@ -17,8 +17,10 @@ struct regex_error : runtime_error {} }; +using RegexBase = boost::basic_regex>; + // Regex that keeps track of its string representation -struct Regex : boost::wregex +struct Regex : RegexBase { Regex() = default; @@ -36,6 +38,10 @@ private: template using RegexUtf8It = utf8::iterator; +template +using RegexIteratorBase = boost::regex_iterator, wchar_t, + boost::c_regex_traits>; + namespace RegexConstant = boost::regex_constants; template @@ -70,19 +76,18 @@ struct MatchResults : boost::match_results> }; template -struct RegexIterator : boost::regex_iterator> +struct RegexIterator : RegexIteratorBase { - using ParentType = boost::regex_iterator>; using Utf8It = RegexUtf8It; using ValueType = MatchResults; RegexIterator() = default; RegexIterator(Iterator begin, Iterator end, const Regex& re, RegexConstant::match_flag_type flags = RegexConstant::match_default) - : ParentType{Utf8It{begin, begin, end}, Utf8It{end, begin, end}, re, flags} {} + : RegexIteratorBase{Utf8It{begin, begin, end}, Utf8It{end, begin, end}, re, flags} {} - const ValueType& operator*() const { return *reinterpret_cast(&ParentType::operator*()); } - const ValueType* operator->() const { return reinterpret_cast(ParentType::operator->()); } + const ValueType& operator*() const { return *reinterpret_cast(&RegexIteratorBase::operator*()); } + const ValueType* operator->() const { return reinterpret_cast(RegexIteratorBase::operator->()); } }; inline RegexConstant::match_flag_type match_flags(bool bol, bool eol, bool bow, bool eow) diff --git a/src/unicode.hh b/src/unicode.hh index 83c63fa1..4ceeac74 100644 --- a/src/unicode.hh +++ b/src/unicode.hh @@ -29,7 +29,7 @@ enum WordType { Word, WORD }; template inline bool is_word(Codepoint c) { - return c == '_' or std::isalnum((wchar_t)c, std::locale{}); + return c == '_' or iswalnum((wchar_t)c); } template<> @@ -68,8 +68,8 @@ inline CharCategories categorize(Codepoint c) return CharCategories::Punctuation; } -inline Codepoint to_lower(Codepoint cp) { return std::tolower((wchar_t)cp, std::locale{}); } -inline Codepoint to_upper(Codepoint cp) { return std::toupper((wchar_t)cp, std::locale{}); } +inline Codepoint to_lower(Codepoint cp) { return towlower((wchar_t)cp); } +inline Codepoint to_upper(Codepoint cp) { return towupper((wchar_t)cp); } inline char to_lower(char c) { return c >= 'A' and c <= 'Z' ? c - 'A' + 'a' : c; } inline char to_upper(char c) { return c >= 'a' and c <= 'z' ? c - 'a' + 'A' : c; }