first commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
MRuby::Gem::Specification.new('mruby-symbol-ext') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'Symbol class extension'
|
||||
end
|
||||
@@ -0,0 +1,72 @@
|
||||
class Symbol
|
||||
include Comparable
|
||||
|
||||
alias intern to_sym
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# sym.capitalize -> symbol
|
||||
#
|
||||
# Same as <code>sym.to_s.capitalize.intern</code>.
|
||||
|
||||
def capitalize
|
||||
(self.to_s.capitalize! || self).to_sym
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# sym.downcase -> symbol
|
||||
#
|
||||
# Same as <code>sym.to_s.downcase.intern</code>.
|
||||
|
||||
def downcase
|
||||
(self.to_s.downcase! || self).to_sym
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# sym.upcase -> symbol
|
||||
#
|
||||
# Same as <code>sym.to_s.upcase.intern</code>.
|
||||
|
||||
def upcase
|
||||
(self.to_s.upcase! || self).to_sym
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# sym.casecmp(other) -> -1, 0, +1 or nil
|
||||
#
|
||||
# Case-insensitive version of <code>Symbol#<=></code>.
|
||||
|
||||
def casecmp(other)
|
||||
return nil unless other.kind_of?(Symbol)
|
||||
lhs = self.to_s; lhs.upcase!
|
||||
rhs = other.to_s.upcase
|
||||
lhs <=> rhs
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# sym.casecmp?(other) -> true, false, or nil
|
||||
#
|
||||
# Returns true if sym and other_sym are equal after case folding,
|
||||
# false if they are not equal, and nil if other_sym is not a string.
|
||||
|
||||
def casecmp?(sym)
|
||||
c = self.casecmp(sym)
|
||||
return nil if c.nil?
|
||||
return c == 0
|
||||
end
|
||||
|
||||
#
|
||||
# call-seq:
|
||||
# sym.empty? -> true or false
|
||||
#
|
||||
# Returns that _sym_ is :"" or not.
|
||||
|
||||
def empty?
|
||||
self.length == 0
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,71 @@
|
||||
#include <mruby.h>
|
||||
#include <mruby/khash.h>
|
||||
#include <mruby/array.h>
|
||||
#include <mruby/string.h>
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* Symbol.all_symbols => array
|
||||
*
|
||||
* Returns an array of all the symbols currently in Ruby's symbol
|
||||
* table.
|
||||
*
|
||||
* Symbol.all_symbols.size #=> 903
|
||||
* Symbol.all_symbols[1,20] #=> [:floor, :ARGV, :Binding, :symlink,
|
||||
* :chown, :EOFError, :$;, :String,
|
||||
* :LOCK_SH, :"setuid?", :$<,
|
||||
* :default_proc, :compact, :extend,
|
||||
* :Tms, :getwd, :$=, :ThreadGroup,
|
||||
* :wait2, :$>]
|
||||
*/
|
||||
#ifdef MRB_ENABLE_ALL_SYMBOLS
|
||||
static mrb_value
|
||||
mrb_sym_all_symbols(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_sym i, lim;
|
||||
mrb_value ary = mrb_ary_new_capa(mrb, mrb->symidx);
|
||||
|
||||
for (i=1, lim=mrb->symidx+1; i<lim; i++) {
|
||||
mrb_sym sym = i<<1;
|
||||
mrb_ary_push(mrb, ary, mrb_symbol_value(sym));
|
||||
}
|
||||
|
||||
return ary;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* sym.length -> integer
|
||||
*
|
||||
* Same as <code>sym.to_s.length</code>.
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_sym_length(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_int len;
|
||||
#ifdef MRB_UTF8_STRING
|
||||
mrb_int byte_len;
|
||||
const char *name = mrb_sym_name_len(mrb, mrb_symbol(self), &byte_len);
|
||||
len = mrb_utf8_strlen(name, byte_len);
|
||||
#else
|
||||
mrb_sym_name_len(mrb, mrb_symbol(self), &len);
|
||||
#endif
|
||||
return mrb_fixnum_value(len);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_symbol_ext_gem_init(mrb_state* mrb)
|
||||
{
|
||||
struct RClass *s = mrb->symbol_class;
|
||||
#ifdef MRB_ENABLE_ALL_SYMBOLS
|
||||
mrb_define_class_method(mrb, s, "all_symbols", mrb_sym_all_symbols, MRB_ARGS_NONE());
|
||||
#endif
|
||||
mrb_define_method(mrb, s, "length", mrb_sym_length, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, s, "size", mrb_sym_length, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_symbol_ext_gem_final(mrb_state* mrb)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
# coding: utf-8
|
||||
##
|
||||
# Symbol(Ext) Test
|
||||
|
||||
if Symbol.respond_to?(:all_symbols)
|
||||
assert('Symbol.all_symbols') do
|
||||
foo = [:__symbol_test_1, :__symbol_test_2, :__symbol_test_3].sort
|
||||
symbols = Symbol.all_symbols.select{|sym|sym.to_s.include? '__symbol_test'}.sort
|
||||
assert_equal foo, symbols
|
||||
end
|
||||
end
|
||||
|
||||
%w[size length].each do |n|
|
||||
assert("Symbol##{n}") do
|
||||
assert_equal 5, :hello.__send__(n)
|
||||
assert_equal 4, :"aA\0b".__send__(n)
|
||||
if __ENCODING__ == "UTF-8"
|
||||
assert_equal 8, :"こんにちは世界!".__send__(n)
|
||||
assert_equal 4, :"aあ\0b".__send__(n)
|
||||
else
|
||||
assert_equal 22, :"こんにちは世界!".__send__(n)
|
||||
assert_equal 6, :"aあ\0b".__send__(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert("Symbol#capitalize") do
|
||||
assert_equal :Hello, :hello.capitalize
|
||||
assert_equal :Hello, :HELLO.capitalize
|
||||
assert_equal :Hello, :Hello.capitalize
|
||||
end
|
||||
|
||||
assert("Symbol#downcase") do
|
||||
assert_equal :hello, :hEllO.downcase
|
||||
assert_equal :hello, :hello.downcase
|
||||
end
|
||||
|
||||
assert("Symbol#upcase") do
|
||||
assert_equal :HELLO, :hEllO.upcase
|
||||
assert_equal :HELLO, :HELLO.upcase
|
||||
end
|
||||
|
||||
assert("Symbol#casecmp") do
|
||||
assert_equal 0, :HELLO.casecmp(:hEllO)
|
||||
assert_equal 1, :HELLO.casecmp(:hEllN)
|
||||
assert_equal(-1, :HELLO.casecmp(:hEllP))
|
||||
assert_nil :HELLO.casecmp("hEllO")
|
||||
end
|
||||
|
||||
assert("Symbol#empty?") do
|
||||
assert_true :''.empty?
|
||||
end
|
||||
|
||||
assert('Symbol#intern') do
|
||||
assert_equal :test, :test.intern
|
||||
end
|
||||
Reference in New Issue
Block a user