Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for String properties
  • Loading branch information
sorear committed Dec 19, 2011
1 parent e09cd3f commit 383966e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
8 changes: 1 addition & 7 deletions lib/Builtins.cs
Expand Up @@ -1534,13 +1534,7 @@ public partial class Builtins {
int r = (int)o1.mo.mro_raw_Numeric.Get(v);
if (r >= 0x110000)
return Kernel.AnyMO.typeVar; // XXX failure
if (r >= 0x10000) {
char[] rs = new char[2];
rs[0] = (char)(0xD800 + ((r - 0x10000) >> 10));
rs[1] = (char)(0xDC00 + (r & 0x3FF));
return Kernel.BoxAnyMO(new string(rs), Kernel.StrMO);
}
return Kernel.BoxAnyMO(new string((char)r, 1), Kernel.StrMO);
return Kernel.BoxAnyMO(Utils.Chr(r), Kernel.StrMO);
}

public static Variable UniCat(Variable v) {
Expand Down
49 changes: 49 additions & 0 deletions lib/UCD.cs
Expand Up @@ -146,6 +146,11 @@ static class DataSet {
(bits[from-2] << 8) | (bits[from-1]);
}

static int Short(ref int from) {
from += 2;
return (bits[from-2] << 8) | (bits[from-1]);
}

static uint BER(ref int from) {
uint buf = 0;
while (true) {
Expand Down Expand Up @@ -278,6 +283,47 @@ static class DataSet {
return new LimitedProperty(data, names.ToArray());
}

static object InflateString(string name, int[] loc) {
int rpos1 = loc[2];
int rpos2 = loc[4];

List<int> codes = new List<int>();
List<string> strs = new List<string>();

while (rpos1 < loc[3]) {
int code = Int(ref rpos1);
int len = code >> 24;
int cp = code & 0xFFFFF;
if ((code & (1 << 23)) != 0) {
char[] buf = new char[len];
for (int i = 0; i < len; i++)
buf[i] = (char)Short(ref rpos2);
codes.Add(cp);
if (len == 1 && buf[0] == 0xD800)
strs.Add("");
else if (len == 1 && buf[0] == 0xD801)
strs.Add(Utils.Chr(cp));
else
strs.Add(new string(buf));
} else {
int rpost = rpos1;
int nextcp = Int(ref rpost) & 0xFFFFF;
while (cp < nextcp) {
char b = (char)Short(ref rpos2);
if (b == (char)0xD800)
strs.Add("");
else if (b == (char)0xD801)
strs.Add(Utils.Chr(cp));
else
strs.Add(new string(b,1));
codes.Add(cp++);
}
}
}

return new StringProperty(codes.ToArray(), strs.ToArray());
}

static object InflateName(string name, int[] loc) {
InflateTokens();
List<int> cps = new List<int>();
Expand Down Expand Up @@ -341,6 +387,9 @@ static class DataSet {
case (byte)'N':
r = InflateName(name, loc);
break;
case (byte)'S':
r = InflateString(name, loc);
break;
default:
throw new NieczaException("Unhandled type code " + (char)bits[loc[0]]);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/Utils.cs
Expand Up @@ -600,6 +600,16 @@ public class Utils {
}
}

public static string Chr(int r) {
if (r >= 0x10000) {
char[] rs = new char[2];
rs[0] = (char)(0xD800 + ((r - 0x10000) >> 10));
rs[1] = (char)(0xDC00 + (r & 0x3FF));
return new string(rs);
}
return new string((char)r, 1);
}

public static string N2S(double n) {
return n.ToString(CultureInfo.InvariantCulture);
}
Expand Down

0 comments on commit 383966e

Please sign in to comment.