Class: CharlockHolmes::Converter

Inherits:
Object
  • Object
show all
Defined in:
ext/charlock_holmes/converter.c

Class Method Summary collapse

Class Method Details

.convert(rb_txt, rb_src_enc, rb_dst_enc) ⇒ Object



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
# File 'ext/charlock_holmes/converter.c', line 7

static VALUE rb_converter_convert(VALUE self, VALUE rb_txt, VALUE rb_src_enc, VALUE rb_dst_enc) {
  VALUE rb_out;
  const char *src_enc;
  const char *dst_enc;
  const char *src_txt;
  char *out_buf;
  void *rb_enc = NULL;
  int32_t src_len;
  int32_t out_len;
  UErrorCode status = U_ZERO_ERROR;

  src_txt = RSTRING_PTR(rb_txt);
  src_len = RSTRING_LEN(rb_txt);
  src_enc = RSTRING_PTR(rb_src_enc);
  dst_enc = RSTRING_PTR(rb_dst_enc);

  // first determin the size of the output buffer
  out_len = ucnv_convert(dst_enc, src_enc, NULL, 0, src_txt, src_len, &status);
  if (status != U_BUFFER_OVERFLOW_ERROR) {
    rb_raise(rb_eArgError, "%s", u_errorName(status));
  }
  out_buf = malloc(out_len);

  // now do the actual conversion
  status = U_ZERO_ERROR;
  out_len = ucnv_convert(dst_enc, src_enc, out_buf, out_len, src_txt, src_len, &status);
  if (U_FAILURE(status)) {
    free(out_buf);
    rb_raise(rb_eArgError, "%s", u_errorName(status));
  }

#ifdef HAVE_RUBY_ENCODING_H
  rb_enc = (void *)rb_enc_find(dst_enc);
#endif

  rb_out = charlock_new_enc_str(out_buf, out_len, rb_enc);

  free(out_buf);

  return rb_out;
}