Difference (last change)
(
Author,
normal page display)
Changed: 1,2c1
Deleted: 192,193d190
stringclasses.d:
 | //Pre-Alpha String Classes
//by Rupert Millard, December 2003
import std.utf;
import std.c.stdio;
alias char utf8byte;
alias dchar utf32char;
interface String
{
void opCatAssign(StringUTF32);
void opCatAssign(StringUTF8);
String opSlice();
//individual character manipulation functions
//these are fast on utf32 strings, slower on utf8 ones
int length();
utf32char opIndex(int i);
void opIndex(int i, utf32char value);
String opSlice(int x, int y);
//this is just for quick and dirty output
//this will not make it to beta
void print();
}
class StringUTF32 : String
{
private utf32char[] chars;
this(StringUTF8 s)
{
chars = toUTF32(s.bytes);
}
this(utf32char[] s)
{
chars = s;
}
void opCatAssign(StringUTF32 s)
{
chars ~= s.chars;
}
void opCatAssign(StringUTF8 s)
{
chars ~= toUTF32(s.bytes);
}
String opSlice()
{
return new StringUTF32(chars[]);
}
int length()
{
return chars.length;
}
utf32char opIndex(int i)
{
return chars[i];
}
void opIndex(int i, utf32char c)
{
chars[i] = c;
}
String opSlice(int x, int y)
{
return new StringUTF32(chars[x .. y]);
}
void print()
{
printf("%.*s\n", toUTF8(chars));
}
}
class StringUTF8 : String
{
public utf8byte[] bytes;
this(utf8byte[] s)
{
bytes = s;
}
this(StringUTF32 s)
{
bytes = toUTF8(s.chars);
}
void opCatAssign(StringUTF32 s)
{
bytes ~= toUTF8(s.chars);
}
void opCatAssign(StringUTF8 s)
{
bytes ~= s.bytes;
}
String opSlice()
{
return new StringUTF8(bytes[]);
}
int length()
{
int n=0;
foreach (utf8byte c; bytes)
{
if(c<0x80 || c>0xBF)
n++;
}
return n;
}
utf32char opIndex(int i)
{
int n=0;
uint p=0;
while (n<i)
{
decode(bytes, p);
n++;
}
return decode(bytes, p);
}
void opIndex(int i, utf32char value)
{
// NOT DONE YET
assert(false);
}
String opSlice(int x, int y)
{
int n=0;
uint px=0, py;
while (n<x)
{
decode(bytes, px);
n++;
}
py=px;
while (n<y)
{
decode(bytes, py);
n++;
}
return new StringUTF8(bytes[px .. py]);
}
void print()
{
printf("%.*s\n", bytes);
}
}
int main (char[][] args)
{
StringUTF8 a = new StringUTF8("String i");
StringUTF32 b = new StringUTF32("n two parts");
a~=b;
a.print();
a=new StringUTF8(b);
a.print();
return 0;
} |
|
|
Rupert Millard, 21 Dec 2003