Browse Source

Fixed snippet argument offsets

pipelines/235045657
eidheim 6 years ago
parent
commit
f3a9d83037
  1. 9
      src/source_base.cpp
  2. 9
      src/utility.cpp
  3. 5
      src/utility.hpp
  4. 5
      tests/utility_test.cpp

9
src/source_base.cpp

@ -1151,7 +1151,8 @@ void Source::BaseView::insert_snippet(Gtk::TextIter iter, const std::string &sni
if(snippet.at(i) != '}') if(snippet.at(i) != '}')
throw std::logic_error("closing } not found"); throw std::logic_error("closing } not found");
++i; ++i;
arguments_offsets[number].emplace_back(insert.size(), insert.size() + placeholder.size()); auto insert_character_count = utf8_character_count(insert);
arguments_offsets[number].emplace_back(insert_character_count, insert_character_count + utf8_character_count(placeholder));
insert += placeholder; insert += placeholder;
} }
else { else {
@ -1170,8 +1171,10 @@ void Source::BaseView::insert_snippet(Gtk::TextIter iter, const std::string &sni
++i; ++i;
} }
} }
else if(parse_number()) else if(parse_number()) {
arguments_offsets[number].emplace_back(insert.size(), insert.size()); auto insert_character_count = utf8_character_count(insert);
arguments_offsets[number].emplace_back(insert_character_count, insert_character_count);
}
else else
parse_variable(); parse_variable();
} }

9
src/utility.cpp

@ -6,6 +6,15 @@ ScopeGuard::~ScopeGuard() {
on_exit(); on_exit();
} }
size_t utf8_character_count(const std::string &text) noexcept {
size_t count = 0;
for(auto chr : text) {
if(static_cast<unsigned char>(chr) <= 0b01111111 || static_cast<unsigned char>(chr) >= 0b11000000)
++count;
}
return count;
}
bool starts_with(const char *str, const std::string &test) noexcept { bool starts_with(const char *str, const std::string &test) noexcept {
for(size_t i = 0; i < test.size(); ++i) { for(size_t i = 0; i < test.size(); ++i) {
if(*str == '\0') if(*str == '\0')

5
src/utility.hpp

@ -8,11 +8,16 @@ public:
~ScopeGuard(); ~ScopeGuard();
}; };
/// Returns number of utf8 characters in text argument
size_t utf8_character_count(const std::string &text) noexcept;
bool starts_with(const char *str, const std::string &test) noexcept; bool starts_with(const char *str, const std::string &test) noexcept;
bool starts_with(const char *str, const char *test) noexcept; bool starts_with(const char *str, const char *test) noexcept;
bool starts_with(const std::string &str, const std::string &test) noexcept; bool starts_with(const std::string &str, const std::string &test) noexcept;
bool starts_with(const std::string &str, const char *test) noexcept; bool starts_with(const std::string &str, const char *test) noexcept;
/// Comparison starts from position pos in str
bool starts_with(const std::string &str, size_t pos, const std::string &test) noexcept; bool starts_with(const std::string &str, size_t pos, const std::string &test) noexcept;
/// Comparison starts from position pos in str
bool starts_with(const std::string &str, size_t pos, const char *test) noexcept; bool starts_with(const std::string &str, size_t pos, const char *test) noexcept;
bool ends_with(const std::string &str, const std::string &test) noexcept; bool ends_with(const std::string &str, const std::string &test) noexcept;

5
tests/utility_test.cpp

@ -11,6 +11,11 @@ int main() {
} }
g_assert(scope_exit); g_assert(scope_exit);
g_assert(utf8_character_count("") == 0);
g_assert(utf8_character_count("test") == 4);
g_assert(utf8_character_count("æøå") == 3);
g_assert(utf8_character_count("æøåtest") == 7);
std::string empty; std::string empty;
std::string test("test"); std::string test("test");
std::string testtest("testtest"); std::string testtest("testtest");

Loading…
Cancel
Save