1 /**
2  * Consoleur: a package for interaction with character-oriented terminal emulators
3  *
4  * Copyright: Maxim Freck, 2017.
5  * Authors:   Maxim Freck <maxim@freck.pp.ru>
6  * License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7  */
8 module consoleur.cli.util;
9 
10 /**
11  * Removes last char from the utf-8 string
12  *
13  * Params:
14  *  str = Source string
15  *
16  * Returns: string whithout last char
17  */
18 string delchar(string str) @safe
19 {
20 	if (str[$-1] < 128) return str[0 .. $-1];
21 
22 	foreach_reverse (n; 0 .. str.length) {
23 		if (str[n] < 0x80 || str[n] > 0xbf) return str[0 .. n];
24 	}
25 
26 	return "";
27 }
28 
29 /**
30  * Params:
31  *  str = string
32  *
33  * Returns: utf-8 string length in characters
34  */
35 size_t utf8length(string str) @safe @nogc
36 {
37 	size_t len = 0;
38 	foreach(ubyte b; str) {
39 		if (b < 0x80 || b > 0xbf) len++;
40 	}
41 	return len;
42 }
43 
44 /**
45  * Repeats symbol `count` times
46  *
47  * Params:
48  *  symbol = Symbol to repeat
49  *  count  = Number of symbol repetitions
50  *
51  * Returns: a string containing `symbol` character a `count` times
52  */
53 string repeat(wchar symbol, uint count) @safe
54 {
55 	string ret;
56 	foreach(_; 0..count) ret~=symbol;
57 	return ret;
58 }