Files
mruby-mruby/mrbgems/mruby-string-ext/src/string.c
T

176 lines
4.5 KiB
C

#include "mruby.h"
#include "mruby/string.h"
#include <ctype.h>
#include <string.h>
static mrb_value
mrb_str_getbyte(mrb_state *mrb, mrb_value str)
{
mrb_int pos;
mrb_get_args(mrb, "i", &pos);
if (pos < 0)
pos += RSTRING_LEN(str);
if (pos < 0 || RSTRING_LEN(str) <= pos)
return mrb_nil_value();
return mrb_fixnum_value((unsigned char)RSTRING_PTR(str)[pos]);
}
/*
* call-seq:
* str.swapcase! -> str or nil
*
* Equivalent to <code>String#swapcase</code>, but modifies the receiver in
* place, returning <i>str</i>, or <code>nil</code> if no changes were made.
* Note: case conversion is effective only in ASCII region.
*/
static mrb_value
mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
{
char *p, *pend;
int modify = 0;
struct RString *s = mrb_str_ptr(str);
mrb_str_modify(mrb, s);
p = s->ptr;
pend = s->ptr + s->len;
while (p < pend) {
if (ISUPPER(*p)) {
*p = TOLOWER(*p);
modify = 1;
}
else if (ISLOWER(*p)) {
*p = TOUPPER(*p);
modify = 1;
}
p++;
}
if (modify) return str;
return mrb_nil_value();
}
/*
* call-seq:
* str.swapcase -> new_str
*
* Returns a copy of <i>str</i> with uppercase alphabetic characters converted
* to lowercase and lowercase characters converted to uppercase.
* Note: case conversion is effective only in ASCII region.
*
* "Hello".swapcase #=> "hELLO"
* "cYbEr_PuNk11".swapcase #=> "CyBeR_pUnK11"
*/
static mrb_value
mrb_str_swapcase(mrb_state *mrb, mrb_value self)
{
mrb_value str;
str = mrb_str_dup(mrb, self);
mrb_str_swapcase_bang(mrb, str);
return str;
}
/*
* call-seq:
* str << integer -> str
* str.concat(integer) -> str
* str << obj -> str
* str.concat(obj) -> str
*
* Append---Concatenates the given object to <i>str</i>. If the object is a
* <code>Integer</code>, it is considered as a codepoint, and is converted
* to a character before concatenation.
*
* a = "hello "
* a << "world" #=> "hello world"
* a.concat(33) #=> "hello world!"
*/
static mrb_value
mrb_str_concat2(mrb_state *mrb, mrb_value self)
{
mrb_value str;
mrb_get_args(mrb, "S", &str);
mrb_str_concat(mrb, self, str);
return self;
}
/*
* call-seq:
* str.start_with?([prefixes]+) -> true or false
*
* Returns true if +str+ starts with one of the +prefixes+ given.
*
* "hello".start_with?("hell") #=> true
*
* # returns true if one of the prefixes matches.
* "hello".start_with?("heaven", "hell") #=> true
* "hello".start_with?("heaven", "paradise") #=> false
*/
static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
{
mrb_value *argv;
int argc, i;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
size_t len_l, len_r, len_cmp;
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(argv[i]);
len_cmp = (len_l > len_r) ? len_r : len_l;
if (memcmp(RSTRING_PTR(self), RSTRING_PTR(argv[i]), len_cmp) == 0) {
return mrb_true_value();
}
}
return mrb_false_value();
}
/*
* call-seq:
* str.end_with?([suffixes]+) -> true or false
*
* Returns true if +str+ ends with one of the +suffixes+ given.
*/
static mrb_value
mrb_str_end_with(mrb_state *mrb, mrb_value self)
{
mrb_value *argv;
int argc, i;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
size_t len_l, len_r, len_cmp;
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(argv[i]);
len_cmp = (len_l > len_r) ? len_r : len_l;
if (memcmp(RSTRING_PTR(self) + (len_l - len_cmp),
RSTRING_PTR(argv[i]) + (len_r - len_cmp),
len_cmp) == 0) {
return mrb_true_value();
}
}
return mrb_false_value();
}
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
struct RClass * s = mrb->string_class;
mrb_define_method(mrb, s, "dump", mrb_str_dump, ARGS_NONE());
mrb_define_method(mrb, s, "getbyte", mrb_str_getbyte, ARGS_REQ(1));
mrb_define_method(mrb, s, "swapcase!", mrb_str_swapcase_bang, ARGS_NONE());
mrb_define_method(mrb, s, "swapcase", mrb_str_swapcase, ARGS_NONE());
mrb_define_method(mrb, s, "concat", mrb_str_concat2, ARGS_REQ(1));
mrb_define_method(mrb, s, "<<", mrb_str_concat2, ARGS_REQ(1));
mrb_define_method(mrb, s, "start_with?", mrb_str_start_with, ARGS_REST());
mrb_define_method(mrb, s, "end_with?", mrb_str_end_with, ARGS_REST());
}
void
mrb_mruby_string_ext_gem_final(mrb_state* mrb)
{
}