1
|
1
|
|
2 // lstring.h
|
|
3 // length-prefixed strings
|
|
4
|
|
5 // Copyright (c) 1999-2002 by Digital Mars
|
|
6 // All Rights Reserved
|
|
7 // written by Walter Bright
|
|
8 // www.digitalmars.com
|
|
9 // License for redistribution is by either the Artistic License
|
|
10 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
11 // See the included readme.txt for details.
|
|
12
|
|
13 #ifndef LSTRING_H
|
|
14 #define LSTRING_H 1
|
|
15
|
|
16 #include "dchar.h"
|
|
17
|
|
18 struct Lstring
|
|
19 {
|
|
20 unsigned length;
|
|
21
|
|
22 // Disable warning about nonstandard extension
|
|
23 #pragma warning (disable : 4200)
|
|
24 dchar string[];
|
|
25
|
|
26 static Lstring zero; // 0 length string
|
|
27
|
|
28 // No constructors because we want to be able to statically
|
|
29 // initialize Lstring's, and Lstrings are of variable size.
|
|
30
|
|
31 #if M_UNICODE
|
|
32 #define LSTRING(p,length) { length, L##p }
|
|
33 #else
|
|
34 #define LSTRING(p,length) { length, p }
|
|
35 #endif
|
|
36
|
|
37 #if __GNUC__
|
|
38 #define LSTRING_EMPTY() { 0 }
|
|
39 #else
|
|
40 #define LSTRING_EMPTY() LSTRING("", 0)
|
|
41 #endif
|
|
42
|
|
43 static Lstring *ctor(const dchar *p) { return ctor(p, Dchar::len(p)); }
|
|
44 static Lstring *ctor(const dchar *p, unsigned length);
|
|
45 static unsigned size(unsigned length) { return sizeof(Lstring) + (length + 1) * sizeof(dchar); }
|
|
46 static Lstring *alloc(unsigned length);
|
|
47 Lstring *clone();
|
|
48
|
|
49 unsigned len() { return length; }
|
|
50
|
|
51 dchar *toDchars() { return string; }
|
|
52
|
|
53 hash_t hash() { return Dchar::calcHash(string, length); }
|
|
54 hash_t ihash() { return Dchar::icalcHash(string, length); }
|
|
55
|
|
56 static int cmp(const Lstring *s1, const Lstring *s2)
|
|
57 {
|
|
58 int c = s2->length - s1->length;
|
|
59 return c ? c : Dchar::memcmp(s1->string, s2->string, s1->length);
|
|
60 }
|
|
61
|
|
62 static int icmp(const Lstring *s1, const Lstring *s2)
|
|
63 {
|
|
64 int c = s2->length - s1->length;
|
|
65 return c ? c : Dchar::memicmp(s1->string, s2->string, s1->length);
|
|
66 }
|
|
67
|
|
68 Lstring *append(const Lstring *s);
|
|
69 Lstring *substring(int start, int end);
|
|
70 };
|
|
71
|
|
72 #endif
|