Implement more sophisticated readline-mode

This commit is contained in:
2021-11-04 13:40:42 -04:00
parent cf5a2d7462
commit deaf4e4873
7 changed files with 150 additions and 64 deletions

View File

@@ -8,6 +8,7 @@
#include <cmath>
#include <iomanip>
#include <cassert>
#include <algorithm>
#ifndef WIN32
#include <unistd.h>
@@ -141,6 +142,25 @@ StringVec split(const std::string &s, char sep) {
return result;
}
std::string repeat_string(const std::string &a, int n) {
int len = a.size();
std::string result(len * n, ' ');
for (int i = 0; i < n; i++) {
for (int j = 0; j < len; j++) {
result[i*len + j] = a[j];
}
}
return result;
}
int common_prefix_length(const std::string &a, const std::string &b) {
int minlen = std::min(a.size(), b.size());
for (int i = 0; i < minlen; i++) {
if (a[i] != b[i]) return i;
}
return minlen;
}
std::string tolower(std::string input) {
for (int i = 0; i < int(input.size()); i++) {
input[i] = std::tolower(input[i]);
@@ -305,6 +325,9 @@ LuaDefine(unittests_util, "c") {
LuaAssert(L, sv2[2]=="");
LuaAssert(L, sv2[3]=="bar");
// Test the repeat string routine.
LuaAssertStrEq(L, util::repeat_string("abc", 3), "abcabcabc");
// test toupper and tolower
LuaAssert(L, util::toupper("fooBar") == "FOOBAR");
LuaAssert(L, util::tolower("fooBar") == "foobar");