File: regexp_property_values.c

package info (click to toggle)
ruby-regexp-property-values 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: ruby: 243; ansic: 51; makefile: 6; sh: 4
file content (62 lines) | stat: -rw-r--r-- 1,568 bytes parent folder | download
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
#include "ruby.h"
#include "ruby/encoding.h"
#include "ruby/oniguruma.h" // still in recent rubies f. backwards compatibility

static int prop_name_to_ctype(char *name, rb_encoding *enc)
{
  UChar *uname;
  int ctype;

  uname = (UChar *)name;
  ctype = ONIGENC_PROPERTY_NAME_TO_CTYPE(enc, uname, uname + strlen(name));
  if (ctype < 0)
    rb_raise(rb_eArgError, "Unknown property name `%s`", name);

  return ctype;
}

VALUE onig_ranges_to_rb(const OnigCodePoint *onig_ranges)
{
  unsigned int range_count, i;
  VALUE result, sub_range;

  range_count = onig_ranges[0];
  result = rb_ary_new2(range_count); // rb_ary_new_capa not avail. in Ruby 2.0

  for (i = 0; i < range_count; i++)
  {
    sub_range = rb_range_new(INT2FIX(onig_ranges[(i * 2) + 1]),
                             INT2FIX(onig_ranges[(i * 2) + 2]),
                             0);
    rb_ary_store(result, i, sub_range);
  }

  return result;
}

VALUE rb_prop_ranges(char *name)
{
  int ctype;
  const OnigCodePoint *onig_ranges;
  OnigCodePoint sb_out;
  rb_encoding *enc;
  enc = rb_utf8_encoding();

  ctype = prop_name_to_ctype(name, enc);
  ONIGENC_GET_CTYPE_CODE_RANGE(enc, ctype, &sb_out, &onig_ranges);
  return onig_ranges_to_rb(onig_ranges);
}

VALUE method_matched_ranges(VALUE self, VALUE arg)
{
  char *prop_name;
  prop_name = StringValueCStr(arg);
  return rb_prop_ranges(prop_name);
}

void Init_regexp_property_values()
{
  VALUE module;
  module = rb_define_module("OnigRegexpPropertyHelper");
  rb_define_singleton_method(module, "matched_ranges", method_matched_ranges, 1);
}