forked from processing-js/processing-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaProxyFunctions.js
More file actions
executable file
·295 lines (267 loc) · 10.2 KB
/
Copy pathJavaProxyFunctions.js
File metadata and controls
executable file
·295 lines (267 loc) · 10.2 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
module.exports = function(virtHashCode, virtEquals, undef) {
return function withProxyFunctions(p, removeFirstArgument) {
/**
* The contains(string) function returns true if the string passed in the parameter
* is a substring of this string. It returns false if the string passed
* in the parameter is not a substring of this string.
*
* @param {String} The string to look for in the current string
*
* @return {boolean} returns true if this string contains
* the string passed as parameter. returns false, otherwise.
*
*/
p.__contains = function (subject, subStr) {
if (typeof subject !== "string") {
return subject.contains.apply(subject, removeFirstArgument(arguments));
}
//Parameter is not null AND
//The type of the parameter is the same as this object (string)
//The javascript function that finds a substring returns 0 or higher
return (
(subject !== null) &&
(subStr !== null) &&
(typeof subStr === "string") &&
(subject.indexOf(subStr) > -1)
);
};
/**
* The __replaceAll() function searches all matches between a substring (or regular expression) and a string,
* and replaces the matched substring with a new substring
*
* @param {String} subject a substring
* @param {String} regex a substring or a regular expression
* @param {String} replace the string to replace the found value
*
* @return {String} returns result
*
* @see #match
*/
p.__replaceAll = function(subject, regex, replacement) {
if (typeof subject !== "string") {
return subject.replaceAll.apply(subject, removeFirstArgument(arguments));
}
return subject.replace(new RegExp(regex, "g"), replacement);
};
/**
* The __replaceFirst() function searches first matche between a substring (or regular expression) and a string,
* and replaces the matched substring with a new substring
*
* @param {String} subject a substring
* @param {String} regex a substring or a regular expression
* @param {String} replace the string to replace the found value
*
* @return {String} returns result
*
* @see #match
*/
p.__replaceFirst = function(subject, regex, replacement) {
if (typeof subject !== "string") {
return subject.replaceFirst.apply(subject, removeFirstArgument(arguments));
}
return subject.replace(new RegExp(regex, ""), replacement);
};
/**
* The __replace() function searches all matches between a substring and a string,
* and replaces the matched substring with a new substring
*
* @param {String} subject a substring
* @param {String} what a substring to find
* @param {String} replacement the string to replace the found value
*
* @return {String} returns result
*/
p.__replace = function(subject, what, replacement) {
if (typeof subject !== "string") {
return subject.replace.apply(subject, removeFirstArgument(arguments));
}
if (what instanceof RegExp) {
return subject.replace(what, replacement);
}
if (typeof what !== "string") {
what = what.toString();
}
if (what === "") {
return subject;
}
var i = subject.indexOf(what);
if (i < 0) {
return subject;
}
var j = 0, result = "";
do {
result += subject.substring(j, i) + replacement;
j = i + what.length;
} while ( (i = subject.indexOf(what, j)) >= 0);
return result + subject.substring(j);
};
/**
* The __equals() function compares two strings (or objects) to see if they are the same.
* This method is necessary because it's not possible to compare strings using the equality operator (==).
* Returns true if the strings are the same and false if they are not.
*
* @param {String} subject a string used for comparison
* @param {String} other a string used for comparison with
*
* @return {boolean} true is the strings are the same false otherwise
*/
p.__equals = function(subject, other) {
if (subject.equals instanceof Function) {
return subject.equals.apply(subject, removeFirstArgument(arguments));
}
return virtEquals(subject, other);
};
/**
* The __equalsIgnoreCase() function compares two strings to see if they are the same.
* Returns true if the strings are the same, either when forced to all lower case or
* all upper case.
*
* @param {String} subject a string used for comparison
* @param {String} other a string used for comparison with
*
* @return {boolean} true is the strings are the same, ignoring case. false otherwise
*/
p.__equalsIgnoreCase = function(subject, other) {
if (typeof subject !== "string") {
return subject.equalsIgnoreCase.apply(subject, removeFirstArgument(arguments));
}
return subject.toLowerCase() === other.toLowerCase();
};
/**
* The __toCharArray() function splits the string into a char array.
*
* @param {String} subject The string
*
* @return {Char[]} a char array
*/
p.__toCharArray = function(subject) {
if (typeof subject !== "string") {
return subject.toCharArray.apply(subject, removeFirstArgument(arguments));
}
var chars = [];
for (var i = 0, len = subject.length; i < len; ++i) {
chars[i] = new Char(subject.charAt(i));
}
return chars;
};
/**
* The __split() function splits a string using the regex delimiter
* specified. If limit is specified, the resultant array will have number
* of elements equal to or less than the limit.
*
* @param {String} subject string to be split
* @param {String} regexp regex string used to split the subject
* @param {int} limit max number of tokens to be returned
*
* @return {String[]} an array of tokens from the split string
*/
p.__split = function(subject, regex, limit) {
if (typeof subject !== "string") {
return subject.split.apply(subject, removeFirstArgument(arguments));
}
var pattern = new RegExp(regex);
// If limit is not specified, use JavaScript's built-in String.split.
if ((limit === undef) || (limit < 1)) {
return subject.split(pattern);
}
// If limit is specified, JavaScript's built-in String.split has a
// different behaviour than Java's. A Java-compatible implementation is
// provided here.
var result = [], currSubject = subject, pos;
while (((pos = currSubject.search(pattern)) !== -1) && (result.length < (limit - 1))) {
var match = pattern.exec(currSubject).toString();
result.push(currSubject.substring(0, pos));
currSubject = currSubject.substring(pos + match.length);
}
if ((pos !== -1) || (currSubject !== "")) {
result.push(currSubject);
}
return result;
};
/**
* The codePointAt() function returns the unicode value of the character at a given index of a string.
*
* @param {int} idx the index of the character
*
* @return {String} code the String containing the unicode value of the character
*/
p.__codePointAt = function(subject, idx) {
var code = subject.charCodeAt(idx),
hi,
low;
if (0xD800 <= code && code <= 0xDBFF) {
hi = code;
low = subject.charCodeAt(idx + 1);
return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
}
return code;
};
/**
* The matches() function checks whether or not a string matches a given regular expression.
*
* @param {String} str the String on which the match is tested
* @param {String} regexp the regexp for which a match is tested
*
* @return {boolean} true if the string fits the regexp, false otherwise
*/
p.__matches = function(str, regexp) {
return (new RegExp(regexp)).test(str);
};
/**
* The startsWith() function tests if a string starts with the specified prefix. If the prefix
* is the empty String or equal to the subject String, startsWith() will also return true.
*
* @param {String} prefix the String used to compare against the start of the subject String.
* @param {int} toffset (optional) an offset into the subject String where searching should begin.
*
* @return {boolean} true if the subject String starts with the prefix.
*/
p.__startsWith = function(subject, prefix, toffset) {
if (typeof subject !== "string") {
return subject.startsWith.apply(subject, removeFirstArgument(arguments));
}
toffset = toffset || 0;
if (toffset < 0 || toffset > subject.length) {
return false;
}
return (prefix === '' || prefix === subject) ? true : (subject.indexOf(prefix) === toffset);
};
/**
* The endsWith() function tests if a string ends with the specified suffix. If the suffix
* is the empty String, endsWith() will also return true.
*
* @param {String} suffix the String used to compare against the end of the subject String.
*
* @return {boolean} true if the subject String starts with the prefix.
*/
p.__endsWith = function(subject, suffix) {
if (typeof subject !== "string") {
return subject.endsWith.apply(subject, removeFirstArgument(arguments));
}
var suffixLen = suffix ? suffix.length : 0;
return (suffix === '' || suffix === subject) ? true :
(subject.indexOf(suffix) === subject.length - suffixLen);
};
/**
* The returns hash code of the.
*
* @param {Object} subject The string
*
* @return {int} a hash code
*/
p.__hashCode = function(subject) {
if (subject.hashCode instanceof Function) {
return subject.hashCode.apply(subject, removeFirstArgument(arguments));
}
return virtHashCode(subject);
};
/**
* The __printStackTrace() prints stack trace to the console.
*
* @param {Exception} subject The error
*/
p.__printStackTrace = function(subject) {
p.println("Exception: " + subject.toString() );
};
};
};