Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
9d520ae7
Commit
9d520ae7
authored
Apr 17, 2014
by
mattn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement String#index, fixes #2073
parent
d000c72b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
5 deletions
+29
-5
mrbgems/mruby-string-utf8/src/string.c
mrbgems/mruby-string-utf8/src/string.c
+21
-5
mrbgems/mruby-string-utf8/test/string.rb
mrbgems/mruby-string-utf8/test/string.rb
+8
-0
No files found.
mrbgems/mruby-string-utf8/src/string.c
View file @
9d520ae7
...
...
@@ -33,11 +33,12 @@ utf8len(unsigned char* p)
}
static
mrb_int
mrb_utf8_strlen
(
mrb_value
str
)
mrb_utf8_strlen
(
mrb_value
str
,
mrb_int
len
)
{
mrb_int
total
=
0
;
unsigned
char
*
p
=
(
unsigned
char
*
)
RSTRING_PTR
(
str
);
unsigned
char
*
e
=
p
+
RSTRING_LEN
(
str
);
unsigned
char
*
e
=
p
;
e
+=
len
<
0
?
RSTRING_LEN
(
str
)
:
len
;
while
(
p
<
e
)
{
p
+=
utf8len
(
p
);
total
++
;
...
...
@@ -48,12 +49,12 @@ mrb_utf8_strlen(mrb_value str)
static
mrb_value
mrb_str_size
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_int
size
=
mrb_utf8_strlen
(
str
);
mrb_int
size
=
mrb_utf8_strlen
(
str
,
-
1
);
return
mrb_fixnum_value
(
size
);
}
#define RSTRING_LEN_UTF8(s) mrb_utf8_strlen(s)
#define RSTRING_LEN_UTF8(s) mrb_utf8_strlen(s
, -1
)
static
mrb_value
noregexp
(
mrb_state
*
mrb
,
mrb_value
self
)
...
...
@@ -245,10 +246,24 @@ mrb_str_aref_m(mrb_state *mrb, mrb_value str)
return
mrb_str_aref
(
mrb
,
str
,
a1
);
}
static
mrb_value
mrb_str_index
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_value
a1
,
a2
;
mrb_int
idx
;
if
(
mrb_get_args
(
mrb
,
"o|o"
,
&
a1
,
&
a2
)
==
1
)
a2
=
mrb_fixnum_value
(
0
);
idx
=
str_index
(
mrb
,
str
,
a1
,
mrb_fixnum
(
a2
));
if
(
idx
!=
-
1
)
return
mrb_fixnum_value
(
mrb_utf8_strlen
(
str
,
idx
));
return
mrb_nil_value
();
}
static
mrb_value
mrb_str_reverse_bang
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_int
utf8_len
=
mrb_utf8_strlen
(
str
);
mrb_int
utf8_len
=
mrb_utf8_strlen
(
str
,
-
1
);
if
(
utf8_len
>
1
)
{
mrb_int
len
=
RSTRING_LEN
(
str
);
char
*
buf
=
(
char
*
)
mrb_malloc
(
mrb
,
(
size_t
)
len
);
...
...
@@ -319,6 +334,7 @@ mrb_mruby_string_utf8_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
s
,
"size"
,
mrb_str_size
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"length"
,
mrb_str_size
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"index"
,
mrb_str_index
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
s
,
"[]"
,
mrb_str_aref_m
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
s
,
"slice"
,
mrb_str_aref_m
,
MRB_ARGS_ANY
());
mrb_define_method
(
mrb
,
s
,
"reverse"
,
mrb_str_reverse
,
MRB_ARGS_NONE
());
...
...
mrbgems/mruby-string-utf8/test/string.rb
View file @
9d520ae7
...
...
@@ -37,3 +37,11 @@ assert('String#size') do
assert_not_equal
str
.
bytesize
,
str
.
size
assert_equal
2
,
str
[
1
,
2
].
size
end
assert
(
'String#index'
)
do
str
=
"こんにちわ世界!
\n
こんにちわ世界!"
assert_nil
str
.
index
(
'さ'
)
assert_equal
3
,
str
.
index
(
'ち'
)
assert_equal
12
,
str
.
index
(
'ち'
,
10
)
assert_equal
nil
,
str
.
index
(
"さ"
)
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment