From 53cb626f49dc202186adf34c585403dc4df54318 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 28 Apr 2014 19:49:00 +0100 Subject: [PATCH] Add an expand_tabs string utility function --- src/string.cc | 18 ++++++++++++++++++ src/string.hh | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/string.cc b/src/string.cc index 46d492f1..91a31802 100644 --- a/src/string.cc +++ b/src/string.cc @@ -1,6 +1,7 @@ #include "string.hh" #include "exception.hh" +#include "utf8_iterator.hh" namespace Kakoune { @@ -111,4 +112,21 @@ bool subsequence_match(StringView str, StringView subseq) return true; } +String expand_tabs(StringView line, CharCount tabstop, CharCount col) +{ + String res; + using Utf8It = utf8::utf8_iterator; + for (Utf8It it = line.begin(); it.base() < line.end(); ++it) + { + if (*it == '\t') + { + CharCount end_col = (col / tabstop + 1) * tabstop; + res += String{' ', end_col - col}; + } + else + res += *it; + } + return res; +} + } diff --git a/src/string.hh b/src/string.hh index fd10c23f..954436e4 100644 --- a/src/string.hh +++ b/src/string.hh @@ -279,6 +279,8 @@ String to_string(const StronglyTypedNumber& val) bool prefix_match(StringView str, StringView prefix); bool subsequence_match(StringView str, StringView subseq); +String expand_tabs(StringView line, CharCount tabstop, CharCount col = 0); + } namespace std