forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasString.cpp
207 lines (176 loc) · 4.55 KB
/
asString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* File: asString.cpp
* Author: ghernan
*
* AsyncScript string class implementation
*
* Created on January 3, 2017, 2:19 PM
*/
#include "ascript_pch.hpp"
#include "asString.h"
#include "jsArray.h"
#include "scriptMain.h"
#include <string>
using namespace std;
Ref<JSClass> createStringClass();
Ref<JSClass> JSString::StringClass = createStringClass();
/**
* Construction function
* @param value
* @return
*/
Ref<JSString> JSString::create(const std::string & value)
{
return refFromNew(new JSString(value));
}
/**
* Strings are never mutable. Therefore 'unFreeze' operation returns a reference
* to the same object.
* @param forceClone
* @return
*/
ASValue JSString::unFreeze(bool forceClone)
{
return ASValue(this);
}
/**
* Tries to transform a string into a double value
* @return
*/
double JSString::toDouble()const
{
const double result = strtod(m_text.c_str(), NULL);
if (result == 0 && !isNumber(m_text))
return getNaN();
else
return result;
}
/**
* Gets the JSON representation of a string
* @param indent
* @return
*/
std::string JSString::getJSON(int indent)
{
return escapeString(m_text, true);
}
/**
* String comparison function.
* @param b
* @param ec
* @return
*/
double JSString::compare (const ASValue& b, ExecutionContext* ec)const
{
return m_text.compare (b.toString(ec));
}
/**
* Member access function overridden to have access to 'length' property, and to
* have access to individual characters.
* @param key
* @return
*/
ASValue JSString::readField(const string& key)const
{
if (key == "length")
return jsDouble (m_text.size());
else
return JSObject::readField(key);
}
/**
* It overrides 'getAt' to have access to individual characters.
* @param index
* @return
*/
ASValue JSString::getAt(ASValue index)
{
if (index.isUint())
{
const size_t uIndex = index.toSizeT();
if (uIndex >= m_text.length())
return jsNull();
else
return jsString(m_text.substr(uIndex, 1));
}
else
return jsNull();
}
ASValue scStringIndexOf(ExecutionContext* ec)
{
string str = ec->getThis().toString(ec);
string search = ec->getParam(0).toString(ec);
size_t p = str.find(search);
int val = (p == string::npos) ? -1 : p;
return jsInt(val);
}
ASValue scStringSubstring(ExecutionContext* ec)
{
string str = ec->getThis().toString(ec);
const size_t lo = ec->getParam(0).toSizeT();
const size_t hi = ec->getParam(1).toSizeT();
size_t l = hi - lo;
if (l > 0 && lo >= 0 && lo + l <= str.length())
return jsString(str.substr(lo, l));
else
return jsString("");
}
ASValue scStringCharAt(ExecutionContext* ec)
{
Ref<JSString> str = ec->getThis().staticCast<JSString>();
return str->getAt( ec->getParam(0) );
}
ASValue scStringCharCodeAt(ExecutionContext* ec)
{
string str = scStringCharAt(ec).toString(ec);
if (!str.empty())
return jsInt(str[0]);
else
return jsInt(0);
}
ASValue scStringSplit(ExecutionContext* ec)
{
//TODO: reuse 'split' at 'utils.h' (this is older)
string str = ec->getThis().toString(ec);
string sep = ec->getParam(0).toString(ec);
Ref<JSArray> result = JSArray::create();
size_t pos = str.find(sep);
while (pos != string::npos)
{
result->push(jsString(str.substr(0, pos)));
str = str.substr(pos + 1);
pos = str.find(sep);
}
if (str.size() > 0)
result->push(jsString(str));
return result->value();
}
ASValue scStringFromCharCode(ExecutionContext* ec)
{
char str[2];
str[0] = (char)ec->getParam(0).toInt32();
str[1] = 0;
return jsString(str);
}
ASValue scStringConstructor(ExecutionContext* ec)
{
return jsString("");
}
/**
* Creates the class object for the 'String' class
* @return
*/
Ref<JSClass> createStringClass()
{
VarMap members;
addNative("function indexOf(search)", scStringIndexOf, members);
addNative("function substring(lo,hi)", scStringSubstring, members);
addNative("function charAt(pos)", scStringCharAt, members);
addNative("function charCodeAt(pos)", scStringCharCodeAt, members);
addNative("function split(separator)", scStringSplit, members);
addNative("function fromCharCode(char)", scStringFromCharCode, members);
return JSClass::createNative("String",
JSObject::DefaultClass,
members,
StringVector(),
scStringConstructor);
}