summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/unicode.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2011-04-01 11:26:55 +0200
committerAdam Borowski <kilobyte@angband.pl>2011-04-01 11:26:55 +0200
commitc42426f17c8ef0ad09900943729300c22497b74b (patch)
tree9ce4c605b63dc5c39cc47919545a0cbe0f6e1421 /crawl-ref/source/unicode.cc
parentdd7ffe0ad54606d84d54f828bcedceb30d2e73b3 (diff)
downloadcrawl-ref-c42426f17c8ef0ad09900943729300c22497b74b.tar.gz
crawl-ref-c42426f17c8ef0ad09900943729300c22497b74b.zip
Correct non-ASCII weapon inscriptions being possibly corrupted.
Diffstat (limited to 'crawl-ref/source/unicode.cc')
-rw-r--r--crawl-ref/source/unicode.cc27
1 files changed, 27 insertions, 0 deletions
diff --git a/crawl-ref/source/unicode.cc b/crawl-ref/source/unicode.cc
index 2fa6c71890..ab6a6b6828 100644
--- a/crawl-ref/source/unicode.cc
+++ b/crawl-ref/source/unicode.cc
@@ -512,6 +512,33 @@ char *next_glyph(char *s)
return s_cur;
}
+std::string chop_string(const char *s, int width, bool spaces = true)
+{
+ const char *s0 = s;
+ ucs_t c;
+
+ while (int clen = utf8towc(&c, s))
+ {
+ int cw = wcwidth(c);
+ // Due to combining chars, we can't stop at merely reaching the
+ // target width, the next character needs to exceed it.
+ if (cw > width) // note: a CJK character might leave one space left
+ break;
+ if (cw >= 0) // should we assert on control chars instead?
+ width -= cw;
+ s += clen;
+ }
+
+ if (spaces && width)
+ return std::string(s0, s - s0) + std::string(width, 0);
+ return std::string(s0, s - s0);;
+}
+
+std::string chop_string(const std::string &s, int width, bool spaces = true)
+{
+ return chop_string(s.c_str(), width, spaces);
+}
+
unsigned short charset_vt100[128] =
{
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,