1
|
1 // lstring.c
|
|
2
|
|
3 // Copyright (c) 1999-2002 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11 #include <stdlib.h>
|
|
12
|
|
13 #include "dchar.h"
|
|
14 #include "mem.h"
|
|
15 #include "lstring.h"
|
|
16
|
|
17 Lstring Lstring::zero = LSTRING_EMPTY();
|
|
18
|
|
19 Lstring *Lstring::ctor(const dchar *p, unsigned length)
|
|
20 {
|
|
21 Lstring *s;
|
|
22
|
|
23 s = alloc(length);
|
|
24 memcpy(s->string, p, length * sizeof(dchar));
|
|
25 return s;
|
|
26 }
|
|
27
|
|
28 Lstring *Lstring::alloc(unsigned length)
|
|
29 {
|
|
30 Lstring *s;
|
|
31
|
|
32 s = (Lstring *)mem.malloc(size(length));
|
|
33 s->length = length;
|
|
34 s->string[length] = 0;
|
|
35 return s;
|
|
36 }
|
|
37
|
|
38 Lstring *Lstring::append(const Lstring *s)
|
|
39 {
|
|
40 Lstring *t;
|
|
41
|
|
42 if (!s->length)
|
|
43 return this;
|
|
44 t = alloc(length + s->length);
|
|
45 memcpy(t->string, string, length * sizeof(dchar));
|
|
46 memcpy(t->string + length, s->string, s->length * sizeof(dchar));
|
|
47 return t;
|
|
48 }
|
|
49
|
|
50 Lstring *Lstring::substring(int start, int end)
|
|
51 {
|
|
52 Lstring *t;
|
|
53
|
|
54 if (start == end)
|
|
55 return &zero;
|
|
56 t = alloc(end - start);
|
|
57 memcpy(t->string, string + start, (end - start) * sizeof(dchar));
|
|
58 return t;
|
|
59 }
|