static void set_encoding_const(const char *name, rb_encoding *enc) { VALUE encoding = rb_enc_from_encoding(enc); char *s = (char *)name; int haslower = 0, hasupper = 0, valid = 0; if (ISDIGIT(*s)) return; if (ISUPPER(*s)) { hasupper = 1; while (*++s && (ISALNUM(*s) || *s == '_')) { if (ISLOWER(*s)) haslower = 1; } } if (!*s) { if (s - name > ENCODING_NAMELEN_MAX) return; valid = 1; rb_define_const(rb_cEncoding, name, encoding); } if (!valid || haslower) { size_t len = s - name; if (len > ENCODING_NAMELEN_MAX) return; if (!haslower || !hasupper) { do { if (ISLOWER(*s)) haslower = 1; if (ISUPPER(*s)) hasupper = 1; } while (*++s && (!haslower || !hasupper)); len = s - name; } len += strlen(s); if (len++ > ENCODING_NAMELEN_MAX) return; MEMCPY(s = ALLOCA_N(char, len), name, char, len); name = s; if (!valid) { if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); for (; *s; ++s) { if (!ISALNUM(*s)) *s = '_'; } if (hasupper) { rb_define_const(rb_cEncoding, name, encoding); } } if (haslower) { for (s = (char *)name; *s; ++s) { if (ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); } rb_define_const(rb_cEncoding, name, encoding); } }
int rb_enc_toupper(int c, rb_encoding *enc) { return (ONIGENC_IS_ASCII_CODE(c)?ONIGENC_ASCII_CODE_TO_UPPER_CASE(c):(c)); }
void Encoding::add_constant(STATE, const char* name, Encoding* enc) { if(ISDIGIT(*name)) return; char* s = const_cast<char*>(name); bool has_lower = false; bool has_upper = false; bool valid = false; if(ISUPPER(*s)) { has_upper = true; while(*++s && (ISALNUM(*s) || *s == '_')) { if(ISLOWER(*s)) has_lower = true; } } if(!*s) { if(s - name > ENCODING_NAMELEN_MAX) return; valid = true; G(encoding)->set_const(state, state->symbol(name), enc); } if(!valid || has_lower) { size_t len = s - name; if(len > ENCODING_NAMELEN_MAX) return; if(!has_lower || !has_upper) { do { if(ISLOWER(*s)) has_lower = true; if(ISUPPER(*s)) has_upper = true; } while(*++s && (!has_lower || !has_upper)); len = s - name; } len += strlen(s); if(len++ > ENCODING_NAMELEN_MAX) return; char* p = s = strdup(name); if(!s) return; name = s; if(!valid) { if(ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); for(; *s; ++s) { if(!ISALNUM(*s)) *s = '_'; } if(has_upper) { G(encoding)->set_const(state, state->symbol(name), enc); } } if(has_lower) { for(s = (char *)name; *s; ++s) { if(ISLOWER(*s)) *s = ONIGENC_ASCII_CODE_TO_UPPER_CASE((int)*s); } G(encoding)->set_const(state, state->symbol(name), enc); } free(p); } }