FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/cext/src/string.cpp at tmpdir-failure · MSNexploder/jruby · GitHub
MSNexploder
/
jruby
Public
forked from
jruby/jruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
jruby
/
cext
/
src
/
string.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
512 lines (421 loc) · 11.7 KB
Breadcrumbs
jruby
/
cext
/
src
/
string.cpp
Copy path
File metadata and controls
512 lines (421 loc) · 11.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
**** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2008-2010 Wayne Meissner
* Copyright (C) 1993-2007 Yukihiro Matsumoto
* Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
* Copyright (C) 2000 Information-technology Promotion Agency, Japan
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK ****
*/
#
include
<
stdio.h
>
#
include
<
stdlib.h
>
#
include
<
string.h
>
#
include
<
sys/param.h
>
#
include
<
jni.h
>
#
include
"
JLocalEnv.h
"
#
include
"
jruby.h
"
#
include
"
JUtil.h
"
#
include
"
ruby.h
"
#
include
"
Handle.h
"
using
namespace
jruby
;
RubyString::RubyString
(JNIEnv* env, jobject obj_): Handle(env, obj_,
T_STRING
)
{
rwdata.
rstring
=
NULL
;
}
RubyString::~RubyString
()
{
RString* rstring = rwdata.
rstring
;
if
(rstring !=
NULL
) {
free
(rstring->
ptr
);
free
(rstring);
}
}
int
RubyString::length
()
{
//
If already synced with java, just return the cached length value
if
(rwdata.
rstring
!=
NULL
) {
return
rwdata.
rstring
->
len
;
}
JLocalEnv env;
jobject byteList = env->
GetObjectField
(obj, RubyString_value_field);
return
env->
GetIntField
(byteList, ByteList_length_field);
}
static
bool
RubyString_jsync
(JNIEnv* env, DataSync* data)
{
return
((RubyString *) data->
data
)->
jsync
(env);
}
static
bool
RubyString_nsync
(JNIEnv* env, DataSync* data)
{
return
((RubyString *) data->
data
)->
nsync
(env);
}
static
bool
RubyString_clean
(JNIEnv* env, DataSync* data)
{
return
((RubyString *) data->
data
)->
clean
(env);
}
RString*
RubyString::toRString
(
bool
readonly)
{
if
(rwdata.
rstring
!=
NULL
) {
if
(readonly || !rwdata.
readonly
) {
return
rwdata.
rstring
;
}
//
Switch from readonly to read-write
rwdata.
readonly
=
false
;
TAILQ_INSERT_TAIL
(&jruby::nsyncq, &rwdata.
nsync
, syncq);
JLocalEnv env;
nsync
(env);
return
rwdata.
rstring
;
}
JLocalEnv env;
rwdata.
jsync
.
data
=
this
;
rwdata.
jsync
.
sync
= RubyString_jsync;
rwdata.
nsync
.
data
=
this
;
rwdata.
nsync
.
sync
= RubyString_nsync;
rwdata.
clean
.
data
=
this
;
rwdata.
clean
.
sync
= RubyString_clean;
rwdata.
rstring
= (RString *)
calloc
(
1
,
sizeof
(RString));
checkExceptions
(env);
rwdata.
readonly
= readonly;
TAILQ_INSERT_TAIL
(&jruby::cleanq, &rwdata.
clean
, syncq);
TAILQ_INSERT_TAIL
(&jruby::jsyncq, &rwdata.
jsync
, syncq);
if
(!readonly) {
TAILQ_INSERT_TAIL
(&jruby::nsyncq, &rwdata.
nsync
, syncq);
}
nsync
(env);
return
rwdata.
rstring
;
}
bool
RubyString::clean
(JNIEnv* env)
{
//
Clear the cached data
rwdata.
readonly
=
false
;
rwdata.
rstring
=
NULL
;
return
false
;
}
bool
RubyString::jsync
(JNIEnv* env)
{
if
(rwdata.
readonly
&& rwdata.
rstring
!=
NULL
) {
//
Don't sync anything, just clear the cached data
rwdata.
rstring
=
NULL
;
rwdata.
readonly
=
false
;
return
false
;
}
if
(rwdata.
rstring
!=
NULL
&& rwdata.
rstring
->
ptr
!=
NULL
) {
jobject byteList = env->
GetObjectField
(obj, RubyString_value_field);
jobject bytes = env->
GetObjectField
(byteList, ByteList_bytes_field);
jint begin = env->
GetIntField
(byteList, ByteList_begin_field);
checkExceptions
(env);
env->
DeleteLocalRef
(byteList);
RString* rstring = rwdata.
rstring
;
env->
SetByteArrayRegion
((jbyteArray) bytes, begin, rstring->
len
,
(jbyte *) rstring->
ptr
);
checkExceptions
(env);
env->
SetIntField
(byteList, ByteList_length_field, rstring->
len
);
env->
DeleteLocalRef
(bytes);
}
return
true
;
}
bool
RubyString::nsync
(JNIEnv* env)
{
jobject byteList = env->
GetObjectField
(obj, RubyString_value_field);
checkExceptions
(env);
jobject bytes = env->
GetObjectField
(byteList, ByteList_bytes_field);
checkExceptions
(env);
jint begin = env->
GetIntField
(byteList, ByteList_begin_field);
checkExceptions
(env);
long
length = env->
GetIntField
(byteList, ByteList_length_field);
checkExceptions
(env);
jint capacity = env->
GetArrayLength
((jarray) bytes) - begin;
checkExceptions
(env);
env->
DeleteLocalRef
(byteList);
RString* rstring = rwdata.
rstring
;
if
((capacity > rstring->
capa
) || (rstring->
capa
==
0
)) {
rstring->
capa
= capacity;
rstring->
ptr
= (
char
*)
realloc
(rstring->
ptr
, rstring->
capa
+
1
);
}
env->
GetByteArrayRegion
((jbyteArray) bytes, begin, length,
(jbyte *) rstring->
ptr
);
checkExceptions
(env);
env->
DeleteLocalRef
(bytes);
rstring->
ptr
[rstring->
len
= length] =
0
;
return
true
;
}
static
VALUE
newString
(
const
char
* ptr,
int
len,
int
capacity =
0
,
bool
tainted =
false
)
{
if
(len <
0
) {
rb_raise
(rb_eArgError,
"
negative string size (or size too big)
"
);
}
JLocalEnv env;
jbyteArray bytes = env->
NewByteArray
(capacity > len ? capacity : len);
checkExceptions
(env);
if
(len >
0
&& ptr !=
NULL
) {
env->
SetByteArrayRegion
(bytes,
0
, len, (jbyte *) ptr);
checkExceptions
(env);
}
jlong result = env->
CallStaticLongMethod
(JRuby_class, JRuby_newString,
jruby::getRuntime
(), bytes, (jint)len, (jboolean) tainted);
checkExceptions
(env);
return
(
VALUE
) result;
}
extern
"
C
"
VALUE
rb_str_new
(
const
char
*ptr,
long
len)
{
return
newString
(ptr, len);
}
#
undef
rb_str_new_cstr
extern
"
C
"
VALUE
rb_str_new_cstr
(
const
char
*ptr)
{
if
(!ptr) {
rb_raise
(rb_eArgError,
"
NULL pointer given
"
);
}
return
newString
(ptr, ptr ?
strlen
(ptr) :
0
);
}
#
define
STR_BUF_MIN_SIZE
1
extern
"
C
"
VALUE
rb_str_buf_new
(
long
capacity)
{
capacity =
MAX
(capacity,
STR_BUF_MIN_SIZE
);
return
newString
(
NULL
,
0
,
MAX
(capacity,
STR_BUF_MIN_SIZE
),
false
);
}
extern
"
C
"
VALUE
rb_str_dup
(
VALUE
str)
{
return
callMethod
(str,
"
dup
"
,
0
,
NULL
);
}
extern
"
C
"
VALUE
rb_str_dup_frozen
(
VALUE
str)
{
return
rb_str_freeze
(
rb_str_dup
(str));
}
extern
"
C
"
VALUE
rb_str_append
(
VALUE
str,
VALUE
str2)
{
return
callMethod
(str,
"
<<
"
,
1
, str2);
}
extern
"
C
"
VALUE
rb_str_cat
(
VALUE
str,
const
char
*ptr,
long
len)
{
if
(len <
0
) {
rb_raise
(rb_eArgError,
"
negative string size (or size too big)
"
);
}
return
rb_str_concat
(str,
rb_str_new
(ptr, len));
}
#
undef
rb_str_cat2
extern
"
C
"
VALUE
rb_str_cat2
(
VALUE
str,
const
char
*ptr)
{
return
rb_str_cat
(str, ptr, ptr ?
strlen
(ptr) :
0
);
}
extern
"
C
"
VALUE
rb_str_concat
(
VALUE
str,
VALUE
other)
{
return
callMethod
(str,
"
concat
"
,
1
, other);
}
extern
"
C
"
VALUE
rb_str_plus
(
VALUE
str1,
VALUE
str2)
{
return
callMethod
(str1,
"
+
"
,
1
, str2);
}
extern
"
C
"
VALUE
rb_str_buf_cat
(
VALUE
str,
const
char
*ptr,
long
len)
{
if
(len ==
0
)
return
str;
if
(len <
0
) {
rb_raise
(rb_eArgError,
"
negative string size (or size too big)
"
);
}
return
callMethod
(str,
"
concat
"
,
1
,
rb_str_new
(ptr, len));
}
#
undef
rb_str_buf_cat2
VALUE
rb_str_buf_cat2
(
VALUE
str,
const
char
*ptr)
{
return
rb_str_buf_cat
(str, ptr, ptr ?
strlen
(ptr) :
0
);
}
VALUE
rb_str_buf_append
(
VALUE
str,
VALUE
str2)
{
return
callMethod
(str,
"
concat
"
,
1
, str2);
}
extern
"
C
"
int
rb_str_cmp
(
VALUE
str1,
VALUE
str2)
{
return
NUM2INT
(
callMethod
(str1,
"
<=>
"
,
1
, str2));
}
extern
"
C
"
void
rb_str_update
(
VALUE
str,
long
beg,
long
len,
VALUE
val)
{
callMethod
(str,
"
[]=
"
,
3
,
LONG2NUM
(beg),
LONG2NUM
(len), val);
}
extern
"
C
"
VALUE
rb_str_inspect
(
VALUE
str)
{
return
callMethod
(str,
"
inspect
"
,
0
);
}
extern
"
C
"
VALUE
rb_str_split
(
VALUE
str,
const
char
*sep)
{
return
callMethod
(str,
"
split
"
,
1
,
rb_str_new2
(sep));
}
extern
"
C
"
VALUE
rb_str2inum
(
VALUE
str,
int
base)
{
return
callMethod
(str,
"
to_i
"
,
1
,
INT2NUM
(base));
}
extern
"
C
"
VALUE
rb_str_substr
(
VALUE
str,
long
beg,
long
len)
{
return
callMethod
(str,
"
[]
"
,
2
,
LONG2NUM
(beg),
LONG2NUM
(len));
}
extern
"
C
"
VALUE
rb_tainted_str_new
(
const
char
* ptr,
long
len)
{
return
newString
(ptr, len, len,
true
);
}
#
undef
rb_tainted_str_new_cstr
extern
"
C
"
VALUE
rb_tainted_str_new_cstr
(
const
char
*ptr)
{
int
len;
if
(!ptr) {
rb_raise
(rb_eArgError,
"
NULL pointer given
"
);
}
len =
strlen
(ptr);
return
newString
(ptr, len, len,
true
);
}
extern
"
C
"
VALUE
rb_str_to_str
(
VALUE
obj)
{
return
rb_convert_type
(obj,
T_STRING
,
"
String
"
,
"
to_str
"
);
}
extern
"
C
"
VALUE
rb_string_value
(
VALUE
* ptr)
{
if
(
TYPE
(*ptr) !=
T_STRING
) {
*ptr =
rb_str_to_str
(*ptr);
}
return
*ptr;
}
extern
"
C
"
char
*
rb_string_value_ptr
(
VALUE
* object_variable)
{
VALUE
str =
rb_string_value
(object_variable);
return
(
char
*) (
RSTRING_PTR
(str));
}
extern
"
C
"
char
*
rb_string_value_cstr
(
VALUE
* object_variable)
{
VALUE
str =
rb_string_value
(object_variable);
long
long
str_size =
NUM2LL
(
callMethod
(str,
"
length
"
,
0
));
char
* cstr = (
char
*) (
RSTRING_PTR
(str));
if
((
unsigned
int
) str_size !=
strlen
(cstr)) {
rb_raise
(rb_eArgError,
"
string contains NULL byte
"
);
}
return
cstr;
}
extern
"
C
"
VALUE
rb_str_freeze
(
VALUE
str)
{
return
callMethodA
(str,
"
freeze
"
,
0
,
NULL
);
}
extern
"
C
"
VALUE
rb_str_intern
(
VALUE
str)
{
return
callMethodA
(str,
"
to_sym
"
,
0
,
NULL
);
}
extern
"
C
"
VALUE
rb_str_length
(
VALUE
str)
{
return
INT2NUM
(
jruby_str_length
(str));
}
extern
"
C
"
VALUE
rb_String
(
VALUE
obj)
{
return
rb_convert_type
(obj,
T_STRING
,
"
String
"
,
"
to_s
"
);
}
extern
"
C
"
VALUE
rb_check_string_type
(
VALUE
val)
{
return
rb_check_convert_type
(val,
0
,
"
String
"
,
"
to_str
"
);
}
extern
"
C
"
VALUE
rb_str_resize
(
VALUE
str,
long
size) {
long
length = (
long
)
jruby_str_length
(str);
if
(size != length) {
JLocalEnv env;
env->
CallVoidMethod
(
valueToObject
(env, str), RubyString_resize_method, (jint)size);
checkExceptions
(env);
jruby_rstring
(str)->
len
= size;
}
return
str;
}
extern
"
C
"
void
rb_str_set_len
(
VALUE
str,
long
size) {
rb_str_resize
(str, size);
}
static
RubyString*
jruby_str
(
VALUE
v)
{
if
(
TYPE
(v) !=
T_STRING
) {
rb_raise
(rb_eTypeError,
"
wrong type (expected String)
"
);
}
return
(RubyString *) v;
}
extern
"
C
"
char
*
jruby_str_cstr
(
VALUE
v)
{
return
jruby_str
(v)->
toRString
(
false
)->
ptr
;
}
extern
"
C
"
char
*
jruby_str_cstr_readonly
(
VALUE
v)
{
return
jruby_str
(v)->
toRString
(
true
)->
ptr
;
}
extern
"
C
"
int
jruby_str_length
(
VALUE
v)
{
return
jruby_str
(v)->
length
();
}
extern
"
C
"
struct
RString
*
jruby_rstring
(
VALUE
v)
{
return
jruby_str
(v)->
toRString
(
false
);
}
extern
"
C
"
char
*
rb_str2cstr
(
VALUE
str,
long
* len)
{
char
* cstr =
RSTRING_PTR
(str);
if
(len) {
*len =
RSTRING_LEN
(str);
}
else
if
((
unsigned
int
)
RSTRING_LEN
(str) !=
strlen
(cstr)) {
rb_warn
(
"
string contains
\\
0 character
"
);
}
return
cstr;
}
Back
|
FazBrowse Home
|
New Git URL