kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

string_utils.cc (7317B)


      1 #include "string_utils.hh"
      2 
      3 #include "exception.hh"
      4 #include "utf8_iterator.hh"
      5 #include "unit_tests.hh"
      6 #include "ranges.hh"
      7 
      8 namespace Kakoune
      9 {
     10 
     11 String trim_indent(StringView str)
     12 {
     13     if (str.empty())
     14         return {};
     15 
     16     if (str[0_byte] == '\n')
     17         str = str.substr(1_byte);
     18     while (not str.empty() and is_blank(str.back()))
     19         str = str.substr(0, str.length() - 1);
     20 
     21     utf8::iterator it{str.begin(), str};
     22     while (it != str.end() and is_horizontal_blank(*it))
     23         ++it;
     24 
     25     const StringView indent{str.begin(), it.base()};
     26     return accumulate(str | split_after<StringView>('\n') | transform([&](auto&& line) {
     27             if (line == "\n")
     28                 return line;
     29             else if (not prefix_match(line, indent))
     30                 throw runtime_error("inconsistent indentation in the string");
     31 
     32             return line.substr(indent.length());
     33         }), String{}, [](String s, StringView l) { return s += l; });
     34 }
     35 
     36 String escape(StringView str, StringView characters, char escape)
     37 {
     38     String res;
     39     res.reserve(str.length());
     40     auto cbeg = characters.begin(), cend = characters.end();
     41     for (auto it = str.begin(), end = str.end(); it != end; )
     42     {
     43         auto next = std::find_first_of(it, end, cbeg, cend);
     44         if (next != end)
     45         {
     46             res += StringView{it, next+1};
     47             res.back() = escape;
     48             res += *next;
     49             it = next+1;
     50         }
     51         else
     52         {
     53             res += StringView{it, next};
     54             break;
     55         }
     56     }
     57     return res;
     58 }
     59 
     60 String unescape(StringView str, StringView characters, char escape)
     61 {
     62     String res;
     63     res.reserve(str.length());
     64     for (auto it = str.begin(), end = str.end(); it != end; )
     65     {
     66         auto next = std::find(it, end, escape);
     67         if (next != end and next+1 != end and contains(characters, *(next+1)))
     68         {
     69             res += StringView{it, next+1};
     70             res.back() = *(next+1);
     71             it = next + 2;
     72         }
     73         else
     74         {
     75             res += StringView{it, next == end ? next : next + 1};
     76             it = next == end ? next : next + 1;
     77         }
     78     }
     79     return res;
     80 }
     81 
     82 String indent(StringView str, StringView indent)
     83 {
     84     String res;
     85     res.reserve(str.length());
     86     bool was_eol = true;
     87     for (ByteCount i = 0; i < str.length(); ++i)
     88     {
     89         if (was_eol)
     90             res += indent;
     91         res += str[i];
     92         was_eol = is_eol(str[i]);
     93     }
     94     return res;
     95 }
     96 
     97 String replace(StringView str, StringView substr, StringView replacement)
     98 {
     99     String res;
    100     for (auto it = str.begin(); it != str.end(); )
    101     {
    102         auto match = std::search(it, str.end(), substr.begin(), substr.end());
    103         res += StringView{it, match};
    104         if (match == str.end())
    105             break;
    106 
    107         res += replacement;
    108         it = match + substr.length();
    109     }
    110     return res;
    111 }
    112 
    113 String left_pad(StringView str, ColumnCount size, Codepoint c)
    114 {
    115     return String(c, std::max(0_col, size - str.column_length())) + str.substr(0, size);
    116 }
    117 
    118 String right_pad(StringView str, ColumnCount size, Codepoint c)
    119 {
    120     return str.substr(0, size) + String(c, std::max(0_col, size - str.column_length()));
    121 }
    122 
    123 Optional<int> str_to_int_ifp(StringView str)
    124 {
    125     bool negative = not str.empty() and str[0] == '-';
    126     if (negative)
    127         str = str.substr(1_byte);
    128     if (str.empty())
    129         return {};
    130 
    131     unsigned int res = 0;
    132     for (auto c : str)
    133     {
    134         if (c < '0' or c > '9')
    135             return {};
    136         res = res * 10 + c - '0';
    137     }
    138     return negative ? -res : res;
    139 }
    140 
    141 int str_to_int(StringView str)
    142 {
    143     if (auto val = str_to_int_ifp(str))
    144         return *val;
    145     throw runtime_error{str + " is not a number"};
    146 }
    147 
    148 bool subsequence_match(StringView str, StringView subseq)
    149 {
    150     auto it = str.begin();
    151     for (auto& c : subseq)
    152     {
    153         if (it == str.end())
    154             return false;
    155         while (*it != c)
    156         {
    157             if (++it == str.end())
    158                 return false;
    159         }
    160         ++it;
    161     }
    162     return true;
    163 }
    164 
    165 String expand_tabs(StringView line, ColumnCount tabstop, ColumnCount col)
    166 {
    167     String res;
    168     res.reserve(line.length());
    169     for (auto it = line.begin(), end = line.end(); it != end; )
    170     {
    171         if (*it == '\t')
    172         {
    173             ColumnCount end_col = (col / tabstop + 1) * tabstop;
    174             res += String{' ', end_col - col};
    175             col = end_col;
    176             ++it;
    177         }
    178         else
    179         {
    180             auto char_beg = it;
    181             auto cp = utf8::read_codepoint(it, end);
    182             res += {char_beg, it};
    183             col += codepoint_width(cp);
    184         }
    185     }
    186     return res;
    187 }
    188 
    189 String double_up(StringView s, StringView characters)
    190 {
    191     String res;
    192     auto pos = s.begin();
    193     for (auto it = s.begin(), end = s.end(); it != end; ++it)
    194     {
    195         if (contains(characters, *it))
    196         {
    197             res += StringView{pos, it+1};
    198             res += *it;
    199             pos = it+1;
    200         }
    201     }
    202     res += StringView{pos, s.end()};
    203     return res;
    204 }
    205 
    206 UnitTest test_string{[]()
    207 {
    208     kak_assert(String("youpi ") + "matin" == "youpi matin");
    209 
    210     kak_assert(StringView{"youpi"}.starts_with(""));
    211     kak_assert(StringView{"youpi"}.starts_with("you"));
    212     kak_assert(StringView{"youpi"}.starts_with("youpi"));
    213     kak_assert(not StringView{"youpi"}.starts_with("youpi!"));
    214 
    215     kak_assert(StringView{"youpi"}.ends_with(""));
    216     kak_assert(StringView{"youpi"}.ends_with("pi"));
    217     kak_assert(StringView{"youpi"}.ends_with("youpi"));
    218     kak_assert(not StringView{"youpi"}.ends_with("oup"));
    219 
    220     kak_assert(trim_indent(" ") == "");
    221     kak_assert(trim_indent("no-indent") == "no-indent");
    222     kak_assert(trim_indent("\nno-indent") == "no-indent");
    223     kak_assert(trim_indent("\n  indent\n  indent") == "indent\nindent");
    224     kak_assert(trim_indent("\n  indent\n    indent") == "indent\n  indent");
    225     kak_assert(trim_indent("\n  indent\n  indent\n   ") == "indent\nindent");
    226 
    227     kak_expect_throw(runtime_error, trim_indent("\n  indent\nno-indent"));
    228 
    229     kak_assert(escape(R"(\youpi:matin:tchou\:)", ":\\", '\\') == R"(\\youpi\:matin\:tchou\\\:)");
    230     kak_assert(unescape(R"(\\youpi\:matin\:tchou\\\:)", ":\\", '\\') == R"(\youpi:matin:tchou\:)");
    231 
    232     kak_assert(prefix_match("tchou kanaky", "tchou"));
    233     kak_assert(prefix_match("tchou kanaky", "tchou kanaky"));
    234     kak_assert(prefix_match("tchou kanaky", "t"));
    235     kak_assert(not prefix_match("tchou kanaky", "c"));
    236 
    237     kak_assert(subsequence_match("tchou kanaky", "tknky"));
    238     kak_assert(subsequence_match("tchou kanaky", "knk"));
    239     kak_assert(subsequence_match("tchou kanaky", "tchou kanaky"));
    240     kak_assert(not subsequence_match("tchou kanaky", "tchou  kanaky"));
    241 
    242     kak_assert(format("Youhou {1} {} '{0:4}' {2:04} \\{}", 10, "hehe", 5) == "Youhou hehe 5 '  10' 0005 {}");
    243 
    244     char buffer[20];
    245     kak_assert(format_to(buffer, "Hey {}", 15) == "Hey 15");
    246 
    247     kak_assert(str_to_int("5") == 5);
    248     kak_assert(str_to_int(to_string(INT_MAX)) == INT_MAX);
    249     kak_assert(str_to_int(to_string(INT_MIN)) == INT_MIN);
    250     kak_assert(str_to_int("00") == 0);
    251     kak_assert(str_to_int("-0") == 0);
    252 
    253     kak_assert(double_up(R"('foo%"bar"')", "'\"%") == R"(''foo%%""bar""'')");
    254 
    255     kak_assert(replace("tchou/tcha/tchi", "/", "!!") == "tchou!!tcha!!tchi");
    256 }};
    257 
    258 }