summaryrefslogtreecommitdiff
path: root/src/string.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2012-10-11 00:41:48 +0200
committerMaxime Coste <frrrwww@gmail.com>2012-10-11 00:41:48 +0200
commit0ce6bd9bf54332d9eed8c7462ab4dfe08f8fac95 (patch)
tree662591856f7227e3fb8592d582edaf2766a3f3e6 /src/string.hh
parent571861bc7bbe10bf831b861f7e6e0a2aa0a40839 (diff)
use ByteCount instead of CharCount when we are really counting bytes
(that is most of the time when we are not concerned with displaying)
Diffstat (limited to 'src/string.hh')
-rw-r--r--src/string.hh17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/string.hh b/src/string.hh
index dfcd9cbb..0b24a15f 100644
--- a/src/string.hh
+++ b/src/string.hh
@@ -3,10 +3,12 @@
#include <string>
#include <iosfwd>
+#include <climits>
#include <boost/regex.hpp>
#include "memoryview.hh"
#include "units.hh"
+#include "utf8.hh"
namespace Kakoune
{
@@ -25,8 +27,11 @@ public:
template<typename Iterator>
String(Iterator begin, Iterator end) : m_content(begin, end) {}
- char operator[](CharCount pos) const { return m_content[(int)pos]; }
- CharCount length() const { return m_content.length(); }
+ char operator[](ByteCount pos) const { return m_content[(int)pos]; }
+ ByteCount length() const { return m_content.length(); }
+ CharCount char_length() const { return utf8::distance(begin(), end()); }
+ ByteCount byte_count_to(CharCount count) const { return utf8::advance(begin(), end(), (int)count) - begin(); }
+ CharCount char_count_to(ByteCount count) const { return utf8::distance(begin(), begin() + (int)count); }
bool empty() const { return m_content.empty(); }
bool operator== (const String& other) const { return m_content == other.m_content; }
@@ -45,7 +50,13 @@ public:
memoryview<char> data() const { return memoryview<char>(m_content.data(), m_content.size()); }
const char* c_str() const { return m_content.c_str(); }
- String substr(CharCount pos, CharCount length = -1) const { return String(m_content.substr((int)pos, (int)length)); }
+ String substr(ByteCount pos, ByteCount length = -1) const { return String(m_content.substr((int)pos, (int)length)); }
+ String substr(CharCount pos, CharCount length = INT_MAX) const
+ {
+ auto b = utf8::advance(begin(), end(), (int)pos);
+ auto e = utf8::advance(b, end(), (int)length);
+ return String(b,e);
+ }
String replace(const String& expression, const String& replacement) const;
using iterator = std::string::const_iterator;