summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/ext/symbol_spec.c
blob: 7d9a7b4379554aa19b3fb93098ee0d0f6f07ba38 (plain)
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
#include "ruby.h"
#include "rubyspec.h"

#include "ruby/encoding.h"

#ifdef __cplusplus
extern "C" {
#endif

VALUE symbol_spec_SYMBOL_P(VALUE self, VALUE obj) {
  return SYMBOL_P(obj) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_intern(VALUE self, VALUE string) {
  return ID2SYM(rb_intern(RSTRING_PTR(string)));
}

VALUE symbol_spec_rb_intern2(VALUE self, VALUE string, VALUE len) {
  return ID2SYM(rb_intern2(RSTRING_PTR(string), FIX2LONG(len)));
}

VALUE symbol_spec_rb_intern_const(VALUE self, VALUE string) {
  return ID2SYM(rb_intern_const(RSTRING_PTR(string)));
}

VALUE symbol_spec_rb_intern_c_compare(VALUE self, VALUE string, VALUE sym) {
  ID symbol = rb_intern(RSTRING_PTR(string));
  return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_intern2_c_compare(VALUE self, VALUE string, VALUE len, VALUE sym) {
  ID symbol = rb_intern2(RSTRING_PTR(string), FIX2LONG(len));
  return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_intern3(VALUE self, VALUE string, VALUE len, VALUE enc) {
  return ID2SYM(rb_intern3(RSTRING_PTR(string), FIX2LONG(len), rb_enc_get(enc)));
}

VALUE symbol_spec_rb_intern3_c_compare(VALUE self, VALUE string, VALUE len, VALUE enc, VALUE sym) {
  ID symbol = rb_intern3(RSTRING_PTR(string), FIX2LONG(len), rb_enc_get(enc));
  return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_id2name(VALUE self, VALUE symbol) {
  const char* c_str = rb_id2name(SYM2ID(symbol));
  return rb_str_new(c_str, strlen(c_str));
}

VALUE symbol_spec_rb_id2str(VALUE self, VALUE symbol) {
  return rb_id2str(SYM2ID(symbol));
}

VALUE symbol_spec_rb_intern_str(VALUE self, VALUE str) {
  return ID2SYM(rb_intern_str(str));
}

VALUE symbol_spec_rb_check_symbol_cstr(VALUE self, VALUE str) {
  return rb_check_symbol_cstr(RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
}

VALUE symbol_spec_rb_is_class_id(VALUE self, VALUE sym) {
  return rb_is_class_id(SYM2ID(sym)) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_is_const_id(VALUE self, VALUE sym) {
  return rb_is_const_id(SYM2ID(sym)) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_is_instance_id(VALUE self, VALUE sym) {
  return rb_is_instance_id(SYM2ID(sym)) ? Qtrue : Qfalse;
}

VALUE symbol_spec_rb_sym2str(VALUE self, VALUE sym) {
  return rb_sym2str(sym);
}

VALUE symbol_spec_rb_to_symbol(VALUE self, VALUE val) {
  return rb_to_symbol(val);
}

void Init_symbol_spec(void) {
  VALUE cls = rb_define_class("CApiSymbolSpecs", rb_cObject);
  rb_define_method(cls, "SYMBOL_P", symbol_spec_SYMBOL_P, 1);
  rb_define_method(cls, "rb_intern", symbol_spec_rb_intern, 1);
  rb_define_method(cls, "rb_intern2", symbol_spec_rb_intern2, 2);
  rb_define_method(cls, "rb_intern_const", symbol_spec_rb_intern_const, 1);
  rb_define_method(cls, "rb_intern_c_compare", symbol_spec_rb_intern_c_compare, 2);
  rb_define_method(cls, "rb_intern2_c_compare", symbol_spec_rb_intern2_c_compare, 3);
  rb_define_method(cls, "rb_intern3", symbol_spec_rb_intern3, 3);
  rb_define_method(cls, "rb_intern3_c_compare", symbol_spec_rb_intern3_c_compare, 4);
  rb_define_method(cls, "rb_id2name", symbol_spec_rb_id2name, 1);
  rb_define_method(cls, "rb_id2str", symbol_spec_rb_id2str, 1);
  rb_define_method(cls, "rb_intern_str", symbol_spec_rb_intern_str, 1);
  rb_define_method(cls, "rb_check_symbol_cstr", symbol_spec_rb_check_symbol_cstr, 1);
  rb_define_method(cls, "rb_is_class_id", symbol_spec_rb_is_class_id, 1);
  rb_define_method(cls, "rb_is_const_id", symbol_spec_rb_is_const_id, 1);
  rb_define_method(cls, "rb_is_instance_id", symbol_spec_rb_is_instance_id, 1);
  rb_define_method(cls, "rb_sym2str", symbol_spec_rb_sym2str, 1);
  rb_define_method(cls, "rb_to_symbol", symbol_spec_rb_to_symbol, 1);
}

#ifdef __cplusplus
}
#endif