ruby-changes:3253
From: ko1@a...
Date: 28 Dec 2007 01:55:43 +0900
Subject: [ruby-changes:3253] nobu - Ruby:r14746 (trunk): * transcode.c (transcode_dispatch): allows transcoding from/to
nobu 2007-12-28 01:55:06 +0900 (Fri, 28 Dec 2007) New Revision: 14746 Modified files: trunk/ChangeLog trunk/transcode.c Log: * transcode.c (transcode_dispatch): allows transcoding from/to ASCII-8BIT. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14746&r2=14745 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/transcode.c?r1=14746&r2=14745 Index: ChangeLog =================================================================== --- ChangeLog (revision 14745) +++ ChangeLog (revision 14746) @@ -1,3 +1,8 @@ +Fri Dec 28 01:55:04 2007 Nobuyoshi Nakada <nobu@r...> + + * transcode.c (transcode_dispatch): allows transcoding from/to + ASCII-8BIT. + Fri Dec 28 01:47:25 2007 NAKAMURA Usaku <usa@r...> * golf_prelude.rb (Integer): Integer is now enumerable on goruby. Index: transcode.c =================================================================== --- transcode.c (revision 14745) +++ transcode.c (revision 14746) @@ -116,8 +116,7 @@ char *const key = transcoder_key(from_encoding, to_encoding); st_data_t k, val = 0; - k = (st_data_t)key; - while (!st_lookup(transcoder_table, k, &val) && + while (!st_lookup(transcoder_table, (k = (st_data_t)key), &val) && st_delete(transcoder_lib_table, &k, &val)) { const char *const lib = (const char *)val; int len = strlen(lib); @@ -258,8 +257,8 @@ rb_transcoding my_transcoding; int final_encoding = 0; - if (argc<1 || argc>2) { - rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); + if (argc < 1 || argc > 2) { + rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc); } if ((to_encidx = rb_to_encoding_index(to_encval = argv[0])) < 0) { to_enc = 0; @@ -288,7 +287,7 @@ return -1; } if (from_enc && to_enc && rb_enc_asciicompat(from_enc) && rb_enc_asciicompat(to_enc)) { - if (ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) { + if (to_encidx == 0 || ENC_CODERANGE(str) == ENC_CODERANGE_7BIT) { return to_encidx; } } @@ -296,14 +295,32 @@ return -1; } - while (!final_encoding) /* loop for multistep transcoding */ - { /* later, maybe use smaller intermediate strings for very long strings */ + if (from_encidx == 0) { + const char *p = RSTRING_PTR(str); + const char *e = p + RSTRING_LEN(str); + + while (p < e) { + int ret = rb_enc_precise_mbclen(p, e, to_enc); + int len = MBCLEN_CHARFOUND(ret); + + if (!len) { + rb_raise(rb_eArgError, "not fully converted, %d bytes left", e-p); + } + p += len; + } + if (to_encidx < 0) { + to_encidx = rb_define_dummy_encoding(to_e); + } + return to_encidx; + } + + while (!final_encoding) { /* loop for multistep transcoding */ + /* later, maybe use smaller intermediate strings for very long strings */ if (!(my_transcoder = transcode_dispatch(from_e, to_e))) { rb_raise(rb_eArgError, "transcoding not supported (from %s to %s)", from_e, to_e); } - if (my_transcoder->preprocessor) - { + if (my_transcoder->preprocessor) { fromp = sp = RSTRING_PTR(str); slen = RSTRING_LEN(str); blen = slen + 30; /* len + margin */ @@ -334,8 +351,7 @@ buf = RSTRING_PTR(dest); *bp = '\0'; rb_str_set_len(dest, bp - buf); - if (my_transcoder->postprocessor) - { + if (my_transcoder->postprocessor) { str = dest; fromp = sp = RSTRING_PTR(str); slen = RSTRING_LEN(str); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml