Exemplo n.º 1
0
  /*
   * This is a primitive so #initialize_copy can work.
   */
  Regexp* Regexp::initialize(STATE, String* pattern, Fixnum* options) {
    const UChar *pat;
    const UChar *end;
    OnigErrorInfo err_info;
    OnigOptionType opts;
    OnigEncoding enc;
    int err, num_names, kcode;

    pat = (UChar*)pattern->byte_address();
    end = pat + pattern->size();

    opts  = options->to_native();
    kcode = opts & KCODE_MASK;
    opts &= OPTION_MASK;

    if(kcode == 0) {
      enc = current_encoding(state);
    } else {
      // Don't attempt to fix the encoding later, it's been specified by the
      // user.
      enc = get_enc_from_kcode(kcode);
      forced_encoding_ = true;
    }

    thread::Mutex::LockGuard lg(state->shared().onig_lock());

    err = onig_new(&this->onig_data, pat, end, opts, enc, ONIG_SYNTAX_RUBY, &err_info);

    if(err != ONIG_NORMAL) {
      UChar onig_err_buf[ONIG_MAX_ERROR_MESSAGE_LEN];
      char err_buf[1024];
      onig_error_code_to_str(onig_err_buf, err, &err_info);
      snprintf(err_buf, 1024, "%s: %s", onig_err_buf, pat);

      Exception::regexp_error(state, err_buf);
      return 0;
    }

    this->source(state, pattern);

    num_names = onig_number_of_names(this->onig_data);

    if(num_names == 0) {
      this->names(state, nil<LookupTable>());
    } else {
      struct _gather_data gd;
      gd.state = state;
      LookupTable* tbl = LookupTable::create(state);
      gd.tbl = tbl;
      onig_foreach_name(this->onig_data, (int (*)(const OnigUChar*, const OnigUChar*,int,int*,OnigRegex,void*))_gather_names, (void*)&gd);
      this->names(state, tbl);
    }

    make_managed(state);

    return this;
  }
Exemplo n.º 2
0
int GetCaptureNames(OnigRegex reg, void *buffer, int bufferSize, int* groupNumbers) {
	int ret;
	group_info_t groupInfo;
	groupInfo.nameBuffer = (char*)buffer;
	groupInfo.bufferOffset = 0;
	groupInfo.bufferSize = bufferSize;
	groupInfo.numbers = groupNumbers;
	groupInfo.numIndex = 0;
	onig_foreach_name(reg, name_callback, (void* )&groupInfo);
	return groupInfo.bufferOffset;
}
Exemplo n.º 3
0
static int 
regexp_main(char *pat0, char *str0) 
{ 
    int r; 
    unsigned char *start, *range, *end; 
    regex_t* reg; 
    OnigErrorInfo einfo; 
    OnigRegion *region; 
     
    static UChar* pattern; 
    static UChar* str; 
    int i; 
     
    pattern = (UChar* )pat0; 
    str = (UChar* )str0; 
     
    strcpy(data_str, str); 
    for (i = 0; i  NAMES; i ++) { 
        data_rslt[i].n[0] = 0; 
    } 
     
    r = onig_new(&reg, pattern, pattern + strlen((char* )pattern), 
                 ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, ONIG_SYNTAX_DEFAULT, &einfo); 
    if (r != ONIG_NORMAL) { 
        char s[ONIG_MAX_ERROR_MESSAGE_LEN]; 
        onig_error_code_to_str(s, r, &einfo); 
        return -1; 
    } 
     
    region = onig_region_new(); 
     
    end   = str + strlen((char* )str); 
    start = str; 
    range = end; 
    r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE); 
    if (r = 0) { 
        r = onig_foreach_name(reg, name_callback, (void* )region); 
    } 
    else if (r == ONIG_MISMATCH) { 
        return 1; 
    } 
    else {                                  /* error */ 
        char s[ONIG_MAX_ERROR_MESSAGE_LEN]; 
        onig_error_code_to_str(s, r); 
        return -1; 
    } 
     
    onig_region_free(region, 1); 
    /* 1:free self, 0:free contents only */ 
    onig_free(reg); 
    onig_end(); 
    return 0; 
} 
Exemplo n.º 4
0
extern int main(int argc, char* argv[])
{
  int r;
  unsigned char *start, *range, *end;
  regex_t* reg;
  OnigErrorInfo einfo;
  OnigRegion *region;

  static UChar* pattern = (UChar* )"(?<foo>a*)(?<bar>b*)(?<foo>c*)";
  static UChar* str = (UChar* )"aaabbbbcc";

  OnigEncoding use_encs[] = { ONIG_ENCODING_ASCII };
  onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));

  r = onig_new(&reg, pattern, pattern + strlen((char* )pattern),
	ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, ONIG_SYNTAX_DEFAULT, &einfo);
  if (r != ONIG_NORMAL) {
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str(s, r, &einfo);
    fprintf(stderr, "ERROR: %s\n", s);
    return -1;
  }

  fprintf(stderr, "number of names: %d\n", onig_number_of_names(reg));

  region = onig_region_new();

  end   = str + strlen((char* )str);
  start = str;
  range = end;
  r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
  if (r >= 0) {
    fprintf(stderr, "match at %d\n\n", r);
    r = onig_foreach_name(reg, name_callback, (void* )region);
  }
  else if (r == ONIG_MISMATCH) {
    fprintf(stderr, "search fail\n");
  }
  else { /* error */
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str(s, r);
    return -1;
  }

  onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
  onig_free(reg);
  onig_end();
  return 0;
}
Exemplo n.º 5
0
/* the target table must be on lua stack top */
static void do_named_subpatterns (lua_State *L, TOnig *ud, const char *text) {
  if (onig_number_of_names (ud->reg) > 0) {
    TNameArg A = { L, ud, text };
    onig_foreach_name(ud->reg, name_callback, &A);
  }
}
Exemplo n.º 6
0
  /*
   * This is a primitive so #initialize_copy can work.
   */
  Regexp* Regexp::initialize(STATE, String* pattern, Fixnum* options) {
    const UChar *pat;
    const UChar *end;
    OnigErrorInfo err_info;
    OnigEncoding enc;

    OnigOptionType opts = options->to_native();

    if(LANGUAGE_18_ENABLED(state)) {
      int kcode = opts & KCODE_MASK;

      pat = (UChar*)pattern->byte_address();
      end = pat + pattern->byte_size();

      if(kcode == 0) {
        enc = pattern->get_encoding_kcode_fallback(state);
      } else {
        // Don't attempt to fix the encoding later, it's been specified by the
        // user.
        enc = get_enc_from_kcode(kcode);
        fixed_encoding_ = true;
      }
    } else {
      fixed_encoding_ = opts & OPTION_FIXEDENCODING;
      no_encoding_    = opts & OPTION_NOENCODING;

      Encoding* source_enc = pattern->encoding(state);

      switch(opts & KCODE_MASK) {
      case KCODE_NONE:
        source_enc = 0;
        no_encoding_ = true;
        break;
      case KCODE_EUC:
        source_enc = Encoding::find(state, "EUC-JP");
        fixed_encoding_ = true;
        break;
      case KCODE_SJIS:
        source_enc = Encoding::find(state, "Windows-31J");
        fixed_encoding_ = true;
        break;
      case KCODE_UTF8:
        source_enc = Encoding::utf8_encoding(state);
        fixed_encoding_ = true;
        break;
      }

      String* converted = pattern->convert_escaped(state, source_enc, fixed_encoding_);

      pat = (UChar*)converted->byte_address();
      end = pat + converted->byte_size();
      enc = source_enc->get_encoding();

      pattern = pattern->string_dup(state);
      pattern->encoding(state, source_enc);
    }

    utilities::thread::Mutex::LockGuard lg(state->shared().onig_lock());

    int err = onig_new(&this->onig_data, pat, end, opts & OPTION_MASK, enc, ONIG_SYNTAX_RUBY, &err_info);

    if(err != ONIG_NORMAL) {
      UChar onig_err_buf[ONIG_MAX_ERROR_MESSAGE_LEN];
      char err_buf[1024];
      onig_error_code_to_str(onig_err_buf, err, &err_info);
      snprintf(err_buf, 1024, "%s: %s", onig_err_buf, pat);

      Exception::regexp_error(state, err_buf);
      return 0;
    }

    this->source(state, pattern);

    int num_names = onig_number_of_names(this->onig_data);

    if(num_names == 0) {
      this->names(state, nil<LookupTable>());
    } else {
      struct _gather_data gd;
      gd.state = state;
      LookupTable* tbl = LookupTable::create(state);
      gd.tbl = tbl;
      onig_foreach_name(this->onig_data, (int (*)(const OnigUChar*, const OnigUChar*,int,int*,OnigRegex,void*))_gather_names, (void*)&gd);
      this->names(state, tbl);
    }

    make_managed(state);

    return this;
  }
Exemplo n.º 7
0
  /*
   * This is a primitive so #initialize_copy can work.
   */
  Regexp* Regexp::initialize(STATE, String* pattern, Fixnum* options) {
    const UChar *pat;
    const UChar *end;
    OnigErrorInfo err_info;
    OnigEncoding enc;

    OnigOptionType opts = options->to_native();
    Encoding* original_enc = pattern->encoding(state);

    fixed_encoding_ = opts & OPTION_FIXEDENCODING;
    no_encoding_    = opts & OPTION_NOENCODING;

    Encoding* source_enc = original_enc;

    switch(opts & KCODE_MASK) {
    case KCODE_NONE:
      no_encoding_ = true;
      break;
    case KCODE_EUC:
      source_enc = Encoding::find(state, "EUC-JP");
      fixed_encoding_ = true;
      break;
    case KCODE_SJIS:
      source_enc = Encoding::find(state, "Windows-31J");
      fixed_encoding_ = true;
      break;
    case KCODE_UTF8:
      source_enc = Encoding::utf8_encoding(state);
      fixed_encoding_ = true;
      break;
    }

    if(no_encoding_) source_enc = 0;
    String* converted = pattern->convert_escaped(state, source_enc, fixed_encoding_);

    pat = (UChar*)converted->byte_address();
    end = pat + converted->byte_size();
    enc = source_enc->get_encoding();

    pattern = pattern->string_dup(state);
    pattern->encoding(state, source_enc);

    regex_t* reg;

    int err = onig_new(&reg, pat, end, opts & OPTION_MASK, enc, ONIG_SYNTAX_RUBY, &err_info);

    if(err != ONIG_NORMAL) {

      enc = original_enc->get_encoding();
      fixed_encoding_ = true;
      err = onig_new(&reg, pat, end, opts & OPTION_MASK, enc, ONIG_SYNTAX_RUBY, &err_info);
      pattern->encoding(state, original_enc);

      if(err != ONIG_NORMAL) {
        UChar onig_err_buf[ONIG_MAX_ERROR_MESSAGE_LEN];
        char err_buf[REGEXP_ONIG_ERROR_MESSAGE_LEN];
        onig_error_code_to_str(onig_err_buf, err, &err_info);
        snprintf(err_buf, REGEXP_ONIG_ERROR_MESSAGE_LEN, "%s: %s", onig_err_buf, pat);

        Exception::regexp_error(state, err_buf);
        return 0;
      }
    }

    this->source(state, pattern);

    int num_names = onig_number_of_names(reg);

    if(num_names == 0) {
      this->names(state, nil<LookupTable>());
    } else {
      struct _gather_data gd;
      gd.state = state;
      LookupTable* tbl = LookupTable::create(state);
      gd.tbl = tbl;
      onig_foreach_name(reg, (int (*)(const OnigUChar*, const OnigUChar*,int,int*,OnigRegex,void*))_gather_names, (void*)&gd);
      this->names(state, tbl);
    }

    make_managed(state, pattern->encoding(), reg);

    return this;
  }