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
4fdf4922
Commit
4fdf4922
authored
Aug 05, 2015
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add String#setbyte and String#byteslice to mruby-string-ext
parent
6f745e21
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
0 deletions
+72
-0
mrbgems/mruby-string-ext/src/string.c
mrbgems/mruby-string-ext/src/string.c
+56
-0
mrbgems/mruby-string-ext/test/string.rb
mrbgems/mruby-string-ext/test/string.rb
+16
-0
No files found.
mrbgems/mruby-string-ext/src/string.c
View file @
4fdf4922
...
...
@@ -3,6 +3,7 @@
#include "mruby/array.h"
#include "mruby/class.h"
#include "mruby/string.h"
#include "mruby/range.h"
static
mrb_value
mrb_str_getbyte
(
mrb_state
*
mrb
,
mrb_value
str
)
...
...
@@ -18,6 +19,59 @@ mrb_str_getbyte(mrb_state *mrb, mrb_value str)
return
mrb_fixnum_value
((
unsigned
char
)
RSTRING_PTR
(
str
)[
pos
]);
}
static
mrb_value
mrb_str_setbyte
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_int
pos
,
byte
;
long
len
=
RSTRING_LEN
(
str
);
mrb_get_args
(
mrb
,
"ii"
,
&
pos
,
&
byte
);
if
(
pos
<
-
len
||
len
<=
pos
)
mrb_raisef
(
mrb
,
E_INDEX_ERROR
,
"index %S is out of array"
,
mrb_fixnum_value
(
pos
));
if
(
pos
<
0
)
pos
+=
len
;
mrb_str_modify
(
mrb
,
mrb_str_ptr
(
str
));
byte
&=
0xff
;
RSTRING_PTR
(
str
)[
pos
]
=
byte
;
return
mrb_fixnum_value
((
unsigned
char
)
byte
);
}
static
mrb_value
mrb_str_byteslice
(
mrb_state
*
mrb
,
mrb_value
str
)
{
mrb_value
a1
;
mrb_int
len
;
int
argc
;
argc
=
mrb_get_args
(
mrb
,
"o|i"
,
&
a1
,
&
len
);
if
(
argc
==
2
)
{
return
mrb_str_substr
(
mrb
,
str
,
mrb_fixnum
(
a1
),
len
);
}
switch
(
mrb_type
(
a1
))
{
case
MRB_TT_RANGE
:
{
mrb_int
beg
;
len
=
RSTRING_LEN
(
str
);
if
(
mrb_range_beg_len
(
mrb
,
a1
,
&
beg
,
&
len
,
len
))
{
return
mrb_str_substr
(
mrb
,
str
,
beg
,
len
);
}
return
mrb_nil_value
();
}
case
MRB_TT_FLOAT
:
a1
=
mrb_fixnum_value
((
mrb_int
)
mrb_float
(
a1
));
/* fall through */
case
MRB_TT_FIXNUM
:
return
mrb_str_substr
(
mrb
,
str
,
mrb_fixnum
(
a1
),
1
);
default:
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"wrong type of argument"
);
}
/* not reached */
return
mrb_nil_value
();
}
/*
* call-seq:
* str.swapcase! -> str or nil
...
...
@@ -375,6 +429,8 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method
(
mrb
,
s
,
"dump"
,
mrb_str_dump
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"getbyte"
,
mrb_str_getbyte
,
MRB_ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
s
,
"setbyte"
,
mrb_str_setbyte
,
MRB_ARGS_REQ
(
2
));
mrb_define_method
(
mrb
,
s
,
"byteslice"
,
mrb_str_byteslice
,
MRB_ARGS_REQ
(
1
)
|
MRB_ARGS_OPT
(
1
));
mrb_define_method
(
mrb
,
s
,
"swapcase!"
,
mrb_str_swapcase_bang
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"swapcase"
,
mrb_str_swapcase
,
MRB_ARGS_NONE
());
mrb_define_method
(
mrb
,
s
,
"concat"
,
mrb_str_concat2
,
MRB_ARGS_REQ
(
1
));
...
...
mrbgems/mruby-string-ext/test/string.rb
View file @
4fdf4922
...
...
@@ -13,6 +13,22 @@ assert('String#getbyte') do
assert_equal
bytes2
[
0
],
str2
.
getbyte
(
0
)
end
assert
(
'String#setbyte'
)
do
str1
=
"hello"
h
=
"H"
.
getbyte
(
0
)
str1
.
setbyte
(
0
,
h
)
assert_equal
(
h
,
str1
.
getbyte
(
0
))
assert_equal
(
"Hello"
,
str1
)
end
assert
(
'String#byteslice'
)
do
str1
=
"hello"
assert_equal
(
"e"
,
str1
.
byteslice
(
1
))
assert_equal
(
"o"
,
str1
.
byteslice
(
-
1
))
assert_equal
(
"ell"
,
str1
.
byteslice
(
1
..
3
))
assert_equal
(
"el"
,
str1
.
byteslice
(
1
...
3
))
end
assert
(
'String#dump'
)
do
(
"
\1
"
*
100
).
dump
# should not raise an exception - regress #1210
"
\0
"
.
inspect
==
"
\"\\
000
\"
"
and
...
...
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