1 /**
2  * This module implements ScriptString. However, host applications should work with D strings by converting
3  * the ScriptAny directly to string.
4  */
5 module mildew.types..string;
6 
7 import mildew.types.object;
8 
9 /**
10  * Encapsulates a UTF-16 string.
11  */
12 class ScriptString : ScriptObject
13 {
14     import std.conv: to;
15 public:
16     /**
17      * Constructs a new ScriptString out of a UTF-8 D string
18      */
19     this(in string str)
20     {
21         import mildew.types.bindings: getStringPrototype;
22         super("string", getStringPrototype, null);
23         _wstring = str.to!wstring;
24     }
25 
26     /**
27      * Returns the actual D string contained
28      */
29     override string toString() const
30     {
31         return _wstring.to!string;
32     }
33 
34     /**
35      * Gets the internally stored UTF-16 string
36      */
37     wstring getWString() const
38     {
39         return _wstring;
40     }
41 
42     // methods to bind
43 
44 package:
45     wchar charAt(size_t index)
46     {
47         if(index >= _wstring.length)
48             return '\0';
49         return _wstring[index];
50     }
51 
52     ushort charCodeAt(size_t index)
53     {
54         if(index >= _wstring.length)
55             return 0;
56         return cast(ushort)(_wstring[index]);
57     }
58 
59 private:
60     wstring _wstring;
61 }