Ticket #46: java-use-string-critical-twice-as-slow-though.patch

File java-use-string-critical-twice-as-slow-though.patch, 2.1 KB (added by Olly Betts, 13 years ago)

Patch to do proper character set conversion for Java

  • xapian-bindings/java/java.i

    diff --git a/xapian-bindings/java/java.i b/xapian-bindings/java/java.i
    index 75c3b05..d93f90e 100644
    a b  
    2929
    3030%include ../xapian-head.i
    3131
     32namespace std {
     33%typemap(in) string
     34%{ if(!$input) {
     35     SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::string");
     36     return $null;
     37    }
     38    size_t $1_jlen = jenv->GetStringLength($input);
     39    const jchar * $1_jstr = jenv->GetStringCritical($input, 0);
     40    if (!$1_jstr) return $null;
     41    $1.resize(0);
     42    for (size_t i = 0; i != $1_jlen; ++i) {
     43        unsigned ch = $1_jstr[i];
     44        if ((ch & 0xfc00) == 0xd800 && ++i != $1_jlen) {
     45            ch = (ch << 10) + $1_jstr[i] - 0x035fdc00;
     46        }
     47        Xapian::Unicode::append_utf8($1, ch);
     48    }
     49    jenv->ReleaseStringCritical($input, $1_jstr); %}
     50
     51%typemap(out) string
     52%{ jchar * $1_ustr = new jchar[$1.size()];
     53   size_t $1_ulen = 0;
     54   for (Xapian::Utf8Iterator $1_uit($1); $1_uit != Xapian::Utf8Iterator(); ++$1_uit) {
     55     unsigned ch = *$1_uit;
     56     if (ch >= 0x10000) {
     57       $1_ustr[$1_ulen++] = 0xdbc0 + (ch >> 10);
     58       ch = 0xdc00 | (ch & 0x3ff);
     59     }
     60     $1_ustr[$1_ulen++] = ch;
     61   }
     62   $result = jenv->NewString($1_ustr, $1_ulen);
     63   delete[] $1_ustr; %}
     64
     65%typemap(in) const string &
     66%{ if(!$input) {
     67     SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::string");
     68     return $null;
     69   }
     70   size_t $1_jlen = jenv->GetStringLength($input);
     71   const jchar * $1_jstr = jenv->GetStringCritical($input, 0);
     72   if (!$1_jstr) return $null;
     73   std::string $1_str;
     74   for (size_t i = 0; i != $1_jlen; ++i) {
     75       unsigned ch = $1_jstr[i];
     76       if ((ch & 0xfc00) == 0xd800 && ++i != $1_jlen) {
     77           ch = (ch << 10) + $1_jstr[i] - 0x035fdc00;
     78       }
     79       Xapian::Unicode::append_utf8($1_str, ch);
     80   }
     81   $1 = &$1_str;
     82   jenv->ReleaseStringCritical($input, $1_jstr); %}
     83}
     84
    3285// Rename function and method names to match Java conventions (e.g. from
    3386// get_description() to getDescription()).
    3487%rename("%(lctitle)s",%$isfunction) "";