Proper unicode support

This commit is contained in:
Zachary Yedidia 2016-03-11 15:37:50 -05:00
parent 24ce1d6b42
commit a74031c1c8
3 changed files with 29 additions and 30 deletions

View File

@ -1,9 +1,9 @@
import std.math; import std.math;
import std.stdio; import std.stdio;
import std.utf; import std.utf: count;
import std.string; import std.string;
import std.conv: to; import std.conv: to;
import std.algorithm; import std.algorithm: min, max;
class Buffer { class Buffer {
private string value = null; private string value = null;
@ -13,7 +13,7 @@ class Buffer {
string name = ""; string name = "";
string savedText; string savedText;
ulong length; ulong count;
int splitLength = 1000; int splitLength = 1000;
int joinLength = 500; int joinLength = 500;
@ -23,7 +23,7 @@ class Buffer {
this(string str, string name = "") { this(string str, string name = "") {
this.value = str; this.value = str;
this.length = str.length; this.count = str.count;
this.name = name; this.name = name;
this.savedText = str; this.savedText = str;
@ -59,13 +59,13 @@ class Buffer {
void adjust() { void adjust() {
if (value !is null) { if (value !is null) {
if (length > splitLength) { if (count > splitLength) {
auto divide = cast(int) floor(length / 2.0); auto divide = cast(int) floor(count / 2.0);
left = new Buffer(value[0 .. divide]); left = new Buffer(value[0 .. divide]);
right = new Buffer(value[divide .. $]); right = new Buffer(value[divide .. $]);
} }
} else { } else {
if (length < joinLength) { if (count < joinLength) {
value = left.toString() ~ right.toString(); value = left.toString() ~ right.toString();
} }
} }
@ -82,20 +82,20 @@ class Buffer {
void remove(ulong start, ulong end) { void remove(ulong start, ulong end) {
if (value !is null) { if (value !is null) {
value = value[0 .. start] ~ value[end .. $]; value = to!string(value.to!dstring[0 .. start] ~ value.to!dstring[end .. $]);
length = value.length; count = value.count;
} else { } else {
auto leftStart = min(start, left.length); auto leftStart = min(start, left.count);
auto leftEnd = min(end, left.length); auto leftEnd = min(end, left.count);
auto rightStart = max(0, min(start - left.length, right.length)); auto rightStart = max(0, min(start - left.count, right.count));
auto rightEnd = max(0, min(end - left.length, right.length)); auto rightEnd = max(0, min(end - left.count, right.count));
if (leftStart < left.length) { if (leftStart < left.count) {
left.remove(leftStart, leftEnd); left.remove(leftStart, leftEnd);
} }
if (rightEnd > 0) { if (rightEnd > 0) {
right.remove(rightStart, rightEnd); right.remove(rightStart, rightEnd);
} }
length = left.length + right.length; count = left.count + right.count;
} }
adjust(); adjust();
@ -103,14 +103,14 @@ class Buffer {
void insert(ulong position, string value) { void insert(ulong position, string value) {
if (this.value !is null) { if (this.value !is null) {
this.value = this.value[0 .. position] ~ value ~ this.value[position .. $]; this.value = to!string(this.value.to!dstring[0 .. position] ~ value.to!dstring ~ this.value.to!dstring[position .. $]);
length = this.value.length; count = this.value.count;
} else { } else {
if (position < left.length) { if (position < left.count) {
left.insert(position, value); left.insert(position, value);
length = left.length + right.length; count = left.count + right.count;
} else { } else {
right.insert(position - left.length, value); right.insert(position - left.count, value);
} }
} }
@ -126,8 +126,8 @@ class Buffer {
void rebalance() { void rebalance() {
if (value is null) { if (value is null) {
if (left.length / right.length > rebalanceRatio || if (left.count / right.count > rebalanceRatio ||
right.length / left.length > rebalanceRatio) { right.count / left.count > rebalanceRatio) {
rebuild(); rebuild();
} else { } else {
left.rebalance(); left.rebalance();
@ -136,14 +136,14 @@ class Buffer {
} }
} }
string substring(ulong start, ulong end = length) { string substring(ulong start, ulong end = count) {
if (value !is null) { if (value !is null) {
return value[start .. end]; return value[start .. end];
} else { } else {
auto leftStart = min(start, left.length); auto leftStart = min(start, left.count);
auto leftEnd = min(end, left.length); auto leftEnd = min(end, left.count);
auto rightStart = max(0, min(start - left.length, right.length)); auto rightStart = max(0, min(start - left.count, right.count));
auto rightEnd = max(0, min(end - left.length, right.length)); auto rightEnd = max(0, min(end - left.count, right.count));
if (leftStart != leftEnd) { if (leftStart != leftEnd) {
if (rightStart != rightEnd) { if (rightStart != rightEnd) {

View File

@ -38,7 +38,6 @@ void main(string[] args) {
sl.display(); sl.display();
flush(); flush();
pollEvent(&e); pollEvent(&e);
v.update(e); v.update(e);
} }

View File

@ -136,12 +136,12 @@ class View {
foreach (_; 0 .. maxLength - lineNum.length) { foreach (_; 0 .. maxLength - lineNum.length) {
setCell(cast(int) x++, cast(int) y, ' ', Color.default_, Color.black); setCell(cast(int) x++, cast(int) y, ' ', Color.default_, Color.black);
} }
foreach (ch; lineNum) { foreach (dchar ch; lineNum) {
setCell(cast(int) x++, cast(int) y, ch, Color.default_, Color.black); setCell(cast(int) x++, cast(int) y, ch, Color.default_, Color.black);
} }
setCell(cast(int) x++, cast(int) y, ' ', Color.default_ | Attribute.bold, Color.black); setCell(cast(int) x++, cast(int) y, ' ', Color.default_ | Attribute.bold, Color.black);
foreach (ch; line) { foreach (dchar ch; line) {
setCell(cast(int) x++, cast(int) y, ch, Color.default_, Color.default_); setCell(cast(int) x++, cast(int) y, ch, Color.default_, Color.default_);
} }
y++; y++;