From 3471e2b1340cb84504272da26051f149f350ee94 Mon Sep 17 00:00:00 2001 From: Mitchell Blank Jr Date: Sat, 19 May 2012 22:40:57 -0700 Subject: C++ compilability - don't define types inside others The following is legal code in both C and C++: struct foo { struct bar { int a } x; int y; }; ...however in C++ it defines a type called "foo::bar" instead of "bar". Just avoid this construct altogether --- src/transcode.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/transcode.c') diff --git a/src/transcode.c b/src/transcode.c index 42c86d167..d6d41b667 100644 --- a/src/transcode.c +++ b/src/transcode.c @@ -65,6 +65,12 @@ allocate_converted_string(mrb_state *mrb, unsigned char *caller_dst_buf, size_t caller_dst_bufsize, size_t *dst_len_ptr); +union mrb_transcoding_state_t { /* opaque data for stateful encoding */ + void *ptr; + char ary[sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*)]; + double dummy_for_alignment; +}; + /* dynamic structure, one per conversion (similar to iconv_t) */ /* may carry conversion state (e.g. for iso-2022-jp) */ typedef struct mrb_transcoding { @@ -92,11 +98,7 @@ typedef struct mrb_transcoding { unsigned char *ptr; /* length: max_output */ } writebuf; - union mrb_transcoding_state_t { /* opaque data for stateful encoding */ - void *ptr; - char ary[sizeof(double) > sizeof(void*) ? sizeof(double) : sizeof(void*)]; - double dummy_for_alignment; - } state; + union mrb_transcoding_state_t state; } mrb_transcoding; #define TRANSCODING_READBUF(tc) \ ((tc)->transcoder->max_input <= (int)sizeof((tc)->readbuf.ary) ? \ -- cgit v1.2.3 From 4ebd0361e4cc6edb0483f3280db50fa56621f0fc Mon Sep 17 00:00:00 2001 From: Mitchell Blank Jr Date: Sat, 19 May 2012 23:16:54 -0700 Subject: Helper functions for st_foreach() return "enum st_retval" not "int" Just making this a bit more consistent throughout the code --- src/encoding.c | 12 ++++++------ src/regparse.c | 10 +++++----- src/st.c | 4 ++-- src/st.h | 2 +- src/transcode.c | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/transcode.c') diff --git a/src/encoding.c b/src/encoding.c index 8a3bd8ef2..cfe21b581 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -913,11 +913,11 @@ enc_name(mrb_state *mrb, mrb_value self) struct fn_arg { mrb_state *mrb; - int (*func)(ANYARGS); + enum st_retval (*func)(ANYARGS); void *a; }; -static int +static enum st_retval fn_i(st_data_t key, st_data_t val, st_data_t arg) { struct fn_arg *a = (struct fn_arg*)arg; @@ -925,7 +925,7 @@ fn_i(st_data_t key, st_data_t val, st_data_t arg) { } static int -st_foreachNew(mrb_state *mrb, st_table *tbl, int (*func)(ANYARGS), void *a) +st_foreachNew(mrb_state *mrb, st_table *tbl, enum st_retval (*func)(ANYARGS), void *a) { struct fn_arg arg = { mrb, @@ -936,7 +936,7 @@ st_foreachNew(mrb_state *mrb, st_table *tbl, int (*func)(ANYARGS), void *a) return st_foreach(tbl, fn_i, (st_data_t)&arg); } -static int +static enum st_retval enc_names_i(mrb_state *mrb, st_data_t name, st_data_t idx, st_data_t args) { mrb_value *arg = (mrb_value *)args; @@ -1521,7 +1521,7 @@ set_encoding_const(mrb_state *mrb, const char *name, mrb_encoding *enc) } } } -static int +static enum st_retval mrb_enc_name_list_i(mrb_state *mrb, st_data_t name, st_data_t idx, mrb_value *arg) { mrb_value ary = *arg; @@ -1554,7 +1554,7 @@ mrb_enc_name_list(mrb_state *mrb, mrb_value klass) return ary; } -static int +static enum st_retval mrb_enc_aliases_enc_i(mrb_state *mrb, st_data_t name, st_data_t orig, st_data_t arg) { mrb_value *p = (mrb_value *)arg; diff --git a/src/regparse.c b/src/regparse.c index ee933662c..6c23f5561 100644 --- a/src/regparse.c +++ b/src/regparse.c @@ -416,7 +416,7 @@ typedef st_data_t HashDataType; /* 1.6 st.h doesn't define st_data_t type */ #define NAMEBUF_SIZE_1 25 #ifdef ONIG_DEBUG -static int +static enum st_retval i_print_name_entry(UChar* key, NameEntry* e, void* arg) { int i; @@ -451,7 +451,7 @@ onig_print_names(FILE* fp, regex_t* reg) } #endif /* ONIG_DEBUG */ -static int +static enum st_retval i_free_name_entry(UChar* key, NameEntry* e, void* arg ARG_UNUSED) { xfree(e->name); @@ -508,7 +508,7 @@ typedef struct { OnigEncoding enc; } INamesArg; -static int +static enum st_retval i_names(UChar* key ARG_UNUSED, NameEntry* e, INamesArg* arg) { int r = (*(arg->func))(e->name, @@ -541,7 +541,7 @@ onig_foreach_name(regex_t* reg, return narg.ret; } -static int +static enum st_retval i_renumber_name(UChar* key ARG_UNUSED, NameEntry* e, GroupNumRemap* map) { int i; @@ -4922,7 +4922,7 @@ static const struct st_hash_type type_type_cclass_hash = { static st_table* OnigTypeCClassTable; -static int +static enum st_retval i_free_shared_class(type_cclass_key* key, Node* node, void* arg ARG_UNUSED) { if (IS_NOT_NULL(node)) { diff --git a/src/st.c b/src/st.c index 777a03bd2..c0597395d 100644 --- a/src/st.c +++ b/src/st.c @@ -404,7 +404,7 @@ st_delete(register st_table *table, register st_data_t *key, st_data_t *value) } int -st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg) +st_foreach(st_table *table, enum st_retval (*func)(ANYARGS), st_data_t arg) { st_table_entry *ptr, **last, *tmp; enum st_retval retval; @@ -572,7 +572,7 @@ numhash(long n) } #if 0 -static int +static enum st_retval f(st_data_t key, st_data_t val, st_data_t a) { printf("tbl=%p key=%s val=%s\n", (st_table*)a, (char*)key, (char*)val); diff --git a/src/st.h b/src/st.h index 24db1fd23..2be618041 100644 --- a/src/st.h +++ b/src/st.h @@ -74,7 +74,7 @@ int st_delete(st_table *, st_data_t *, st_data_t *); int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t); int st_insert(st_table *, st_data_t, st_data_t); int st_lookup(st_table *, st_data_t, st_data_t *); -int st_foreach(st_table *, int (*)(ANYARGS), st_data_t); +int st_foreach(st_table *, enum st_retval (*)(ANYARGS), st_data_t); void st_add_direct(st_table *, st_data_t, st_data_t); void st_free_table(st_table *); void st_cleanup_safe(st_table *, st_data_t); diff --git a/src/transcode.c b/src/transcode.c index d6d41b667..39dc42bdc 100644 --- a/src/transcode.c +++ b/src/transcode.c @@ -273,7 +273,7 @@ typedef struct { const char *base_enc; } search_path_bfs_t; -static int +static enum st_retval transcode_search_path_i(st_data_t key, st_data_t val, st_data_t arg) { const char *dname = (const char *)key; @@ -1797,7 +1797,7 @@ struct asciicompat_encoding_t { const char *ascii_incompat_name; }; -static int +static enum st_retval asciicompat_encoding_i(mrb_state *mrb, st_data_t key, st_data_t val, st_data_t arg) { struct asciicompat_encoding_t *data = (struct asciicompat_encoding_t *)arg; -- cgit v1.2.3 From be0e3fdf5159006277bad7acf1e38fa66efab0d7 Mon Sep 17 00:00:00 2001 From: Mitchell Blank Jr Date: Sat, 19 May 2012 23:18:47 -0700 Subject: C++ compilability -- "try" is a keyword, avoid as variable name --- src/transcode.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/transcode.c') diff --git a/src/transcode.c b/src/transcode.c index 39dc42bdc..ae78f20d4 100644 --- a/src/transcode.c +++ b/src/transcode.c @@ -1127,16 +1127,16 @@ trans_sweep(mrb_state *mrb, mrb_econv_t *ec, int flags, int start) { - int try; + int should_try; int i, f; const unsigned char **ipp, *is, *iold; unsigned char **opp, *os, *oold; mrb_econv_result_t res; - try = 1; - while (try) { - try = 0; + should_try = 1; + while (should_try) { + should_try = 0; for (i = start; i < ec->num_trans; i++) { mrb_econv_elem_t *te = &ec->elems[i]; @@ -1179,7 +1179,7 @@ trans_sweep(mrb_state *mrb, mrb_econv_t *ec, oold = *opp; te->last_result = res = mrb_transcoding_convert(mrb, te->tc, ipp, is, opp, os, f); if (iold != *ipp || oold != *opp) - try = 1; + should_try = 1; switch (res) { case econv_invalid_byte_sequence: -- cgit v1.2.3 From 42c6b324db6141d05726beec531ae19bd83e85f0 Mon Sep 17 00:00:00 2001 From: Mitchell Blank Jr Date: Sun, 20 May 2012 00:06:49 -0700 Subject: C++ compilability -- don't jump across variable initialization C++ is picker than C about when you can "goto" across a variable being defined. The fix is to just minimize the variable's scope inside an extra set of brackets. Without this change, g++ has the following errors: transcode.c:590: error: jump to label 'resume_label3' transcode.c:514: error: from here transcode.c:582: error: crosses initialization of 'const unsigned char* p' transcode.c:2124: error: jump to label 'set_encs' transcode.c:2184: error: from here transcode.c:2088: error: skips initialization of 'const char* err' transcode.c:2089: error: skips initialization of 'size_t error_len' transcode.c:2090: error: skips initialization of 'mrb_value bytes' transcode.c:2091: error: skips initialization of 'mrb_value dumped' transcode.c:2092: error: skips initialization of 'size_t readagain_len' transcode.c:2093: error: skips initialization of 'mrb_value bytes2' --- src/transcode.c | 93 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 44 deletions(-) (limited to 'src/transcode.c') diff --git a/src/transcode.c b/src/transcode.c index ae78f20d4..e9d9b01ef 100644 --- a/src/transcode.c +++ b/src/transcode.c @@ -579,10 +579,12 @@ transcode_restartable0(mrb_state *mrb, switch (mrb_fixnum(next_info) & 0x1F) { case NOMAP: { - const unsigned char *p = inchar_start; - writebuf_off = 0; - while (p < in_p) { - TRANSCODING_WRITEBUF(tc)[writebuf_off++] = (unsigned char)*p++; + { + const unsigned char *p = inchar_start; + writebuf_off = 0; + while (p < in_p) { + TRANSCODING_WRITEBUF(tc)[writebuf_off++] = (unsigned char)*p++; + } } writebuf_len = writebuf_off; writebuf_off = 0; @@ -2085,51 +2087,54 @@ make_econv_exception(mrb_state *mrb, mrb_econv_t *ec) mrb_value mesg, exc; if (ec->last_error.result == econv_invalid_byte_sequence || ec->last_error.result == econv_incomplete_input) { - const char *err = (const char *)ec->last_error.error_bytes_start; - size_t error_len = ec->last_error.error_bytes_len; - mrb_value bytes = mrb_str_new(mrb, err, error_len); - mrb_value dumped = mrb_str_dump(mrb, bytes); - size_t readagain_len = ec->last_error.readagain_len; - mrb_value bytes2 = mrb_nil_value(); - mrb_value dumped2; - int idx; - if (ec->last_error.result == econv_incomplete_input) { - mesg = mrb_sprintf(mrb, "incomplete %s on %s", - //StringValueCStr(dumped), - mrb_string_value_cstr(mrb, &dumped), - ec->last_error.source_encoding); - } - else if (readagain_len) { - bytes2 = mrb_str_new(mrb, err+error_len, readagain_len); - dumped2 = mrb_str_dump(mrb, bytes2); - mesg = mrb_sprintf(mrb, "%s followed by %s on %s", - //StringValueCStr(dumped), - mrb_string_value_cstr(mrb, &dumped), - //StringValueCStr(dumped2), - mrb_string_value_cstr(mrb, &dumped2), - ec->last_error.source_encoding); - } - else { - mesg = mrb_sprintf(mrb, "%s on %s", - //StringValueCStr(dumped), - mrb_string_value_cstr(mrb, &dumped), - ec->last_error.source_encoding); + { + const char *err = (const char *)ec->last_error.error_bytes_start; + size_t error_len = ec->last_error.error_bytes_len; + mrb_value bytes = mrb_str_new(mrb, err, error_len); + mrb_value dumped = mrb_str_dump(mrb, bytes); + size_t readagain_len = ec->last_error.readagain_len; + mrb_value bytes2 = mrb_nil_value(); + mrb_value dumped2; + if (ec->last_error.result == econv_incomplete_input) { + mesg = mrb_sprintf(mrb, "incomplete %s on %s", + //StringValueCStr(dumped), + mrb_string_value_cstr(mrb, &dumped), + ec->last_error.source_encoding); + } + else if (readagain_len) { + bytes2 = mrb_str_new(mrb, err+error_len, readagain_len); + dumped2 = mrb_str_dump(mrb, bytes2); + mesg = mrb_sprintf(mrb, "%s followed by %s on %s", + //StringValueCStr(dumped), + mrb_string_value_cstr(mrb, &dumped), + //StringValueCStr(dumped2), + mrb_string_value_cstr(mrb, &dumped2), + ec->last_error.source_encoding); + } + else { + mesg = mrb_sprintf(mrb, "%s on %s", + //StringValueCStr(dumped), + mrb_string_value_cstr(mrb, &dumped), + ec->last_error.source_encoding); + } + + exc = mrb_exc_new3(mrb, E_INVALIDBYTESEQUENCE_ERROR, mesg); + mrb_iv_set(mrb, exc, mrb_intern(mrb, "error_bytes"), bytes); + mrb_iv_set(mrb, exc, mrb_intern(mrb, "readagain_bytes"), bytes2); + mrb_iv_set(mrb, exc, mrb_intern(mrb, "incomplete_input"), ec->last_error.result == econv_incomplete_input ? mrb_true_value() : mrb_false_value()); } - exc = mrb_exc_new3(mrb, E_INVALIDBYTESEQUENCE_ERROR, mesg); - mrb_iv_set(mrb, exc, mrb_intern(mrb, "error_bytes"), bytes); - mrb_iv_set(mrb, exc, mrb_intern(mrb, "readagain_bytes"), bytes2); - mrb_iv_set(mrb, exc, mrb_intern(mrb, "incomplete_input"), ec->last_error.result == econv_incomplete_input ? mrb_true_value() : mrb_false_value()); - set_encs: mrb_iv_set(mrb, exc, mrb_intern(mrb, "source_encoding_name"), mrb_str_new2(mrb, ec->last_error.source_encoding)); mrb_iv_set(mrb, exc, mrb_intern(mrb, "destination_encoding_name"), mrb_str_new2(mrb, ec->last_error.destination_encoding)); - idx = mrb_enc_find_index(mrb, ec->last_error.source_encoding); - if (0 <= idx) - mrb_iv_set(mrb, exc, mrb_intern(mrb, "source_encoding"), mrb_enc_from_encoding(mrb, mrb_enc_from_index(mrb, idx))); - idx = mrb_enc_find_index(mrb, ec->last_error.destination_encoding); - if (0 <= idx) - mrb_iv_set(mrb, exc, mrb_intern(mrb, "destination_encoding"), mrb_enc_from_encoding(mrb, mrb_enc_from_index(mrb, idx))); + { + int idx = mrb_enc_find_index(mrb, ec->last_error.source_encoding); + if (0 <= idx) + mrb_iv_set(mrb, exc, mrb_intern(mrb, "source_encoding"), mrb_enc_from_encoding(mrb, mrb_enc_from_index(mrb, idx))); + idx = mrb_enc_find_index(mrb, ec->last_error.destination_encoding); + if (0 <= idx) + mrb_iv_set(mrb, exc, mrb_intern(mrb, "destination_encoding"), mrb_enc_from_encoding(mrb, mrb_enc_from_index(mrb, idx))); + } return exc; } if (ec->last_error.result == econv_undefined_conversion) { -- cgit v1.2.3