1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
/*
* Anthy内部で使う文字列の処理
* typedef struct xstr_ {
* xstr *str; int len;
* } xstr;
*
* malloc(0);の意味は考えないで0文字の文字列を扱えるような
* コーディングをする。free(0)は良い。
*
* デフォルトの設定では
* cstrはCの普通のEUC文字列
*
* Copyright (C) 2000-2007 TABATA Yusuke
*
*/
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
/* for ANTHY_*_ENCODING */
#include <anthy/anthy.h>
#include <anthy/xstr.h>
#include <anthy/xchar.h>
#include "diclib_inner.h"
/* 画面に出力するときのエンコーディング */
static int print_encoding;
#define MAX_BYTES_PER_XCHAR 10
static int
xc_isprint(xchar xc)
{
return xc > 0;
}
/** Cの文字列に対応するxstrの長さを計算する
*/
static int
xlengthofcstr(const char *c)
{
int ll = 0;
int len = strlen(c);
int i;
for (i = 0; i < len; i++) {
ll ++;
if ((c[i] & 0x80)) {
i++;
}
}
return ll;
}
const char *
anthy_utf8_to_ucs4_xchar(const char *s, xchar *res)
{
const unsigned char *str = (const unsigned char *)s;
int i, len;
xchar cur;
cur = str[0];
if (str[0] < 0x80) {
len = 1;
} else if (str[0] < 0xe0) {
cur &= 0x1f;
len = 2;
} else if (str[0] < 0xf0) {
cur &= 0x0f;
len = 3;
} else if (str[0] < 0xf8) {
cur &= 0x07;
len = 4;
} else if (str[0] < 0xfc) {
cur &= 0x03;
len = 5;
} else {
cur &= 0x01;
len = 6;
}
str ++;
for (i = 1; i < len; i++) {
cur <<= 6;
cur |= (str[0] & 0x3f);
str++;
}
*res = cur;
return (const char *)str;
}
static xstr *
utf8_to_ucs4_xstr(const char *s)
{
const unsigned char *str = (const unsigned char *)s;
xstr res;
res.str = (xchar *)alloca(sizeof(xchar) * strlen(s));
res.len = 0;
while (*str) {
xchar cur;
str = (const unsigned char *)anthy_utf8_to_ucs4_xchar((const char *)str,
&cur);
res.str[res.len] = cur;
res.len ++;
}
return anthy_xstr_dup(&res);
}
static int
put_xchar_to_utf8_str(xchar xc, char *buf_)
{
int i, len;
unsigned char *buf = (unsigned char *)buf_;
if (xc < 0x80) {
buf[0] = 0;
len = 1;
} else if (xc < 0x800) {
buf[0] = 0xc0;
len = 2;
} else if (xc < 0x10000) {
buf[0] = 0xe0;
len = 3;
} else if (xc < 0x200000) {
buf[0] = 0xf0;
len = 4;
} else if (xc < 0x400000) {
buf[0] = 0xf8;
len = 5;
} else {
buf[0] = 0xfc;
len = 6;
}
for (i = len - 1; i > 0; i--) {
buf[i] = (xc & 0x3f) | 0x80;
xc >>= 6;
}
buf[0] += xc;
buf[len] = 0;
return len;
}
static char *
ucs4_xstr_to_utf8(xstr *xs)
{
char *buf = alloca(xs->len * 6 + 1);
int i, t = 0;
buf[0] = 0;
for (i = 0; i < xs->len; i++) {
xchar xc = xs->str[i];
put_xchar_to_utf8_str(xc, &buf[t]);
t = strlen(buf);
}
return strdup(buf);
}
/** Cの文字列をxstrに変更する
*/
xstr *
anthy_cstr_to_xstr(const char *c, int encoding)
{
xstr *x;
int i, j, l;
if (encoding == ANTHY_UTF8_ENCODING) {
return utf8_to_ucs4_xstr(c);
}
l = xlengthofcstr(c);
x = (xstr *)malloc(sizeof(struct xstr_));
if (!x) {
return NULL;
}
x->len = l;
x->str = malloc(sizeof(xchar)*l);
for (i = 0, j = 0; i < l; i++) {
if (!(c[j] & 0x80)){
x->str[i] = c[j];
j++;
} else {
unsigned char *p = (unsigned char *)&c[j];
x->str[i] = (p[1] | (p[0]<<8)) | 0x8080;
x->str[i] = anthy_euc_to_ucs(x->str[i]);
j++;
j++;
}
}
return x;
}
char *
anthy_xstr_to_cstr(xstr *s, int encoding)
{
int i, j, l;
char *p;
if (encoding == ANTHY_UTF8_ENCODING) {
return ucs4_xstr_to_utf8(s);
}
l = s->len;
for (i = 0; i < s->len; i++) {
int ec = anthy_ucs_to_euc(s->str[i]);
if (ec > 255) {
l++;
}
}
p = (char *)malloc(l + 1);
p[l] = 0;
j = 0;
for (i = 0; i < s->len; i++) {
int ec = anthy_ucs_to_euc(s->str[i]);
if (ec < 256) {
p[j] = ec;
j++;
}else{
p[j] = ec >> 8;
j++;
p[j] = ec & 255;
j++;
}
}
return p;
}
xstr *
anthy_xstr_dup(xstr *s)
{
int i;
xstr *x = (xstr *)malloc(sizeof(xstr));
x->len = s->len;
if (s->len) {
x->str = malloc(sizeof(xchar)*s->len);
}else{
x->str = NULL;
}
for (i = 0; i < x->len; i++) {
x->str[i] = s->str[i];
}
return x;
}
xchar *
anthy_xstr_dup_str(xstr *s)
{
xchar *c;
int i;
if (s->len) {
c = malloc(sizeof(xchar)*s->len);
}else{
c = 0;
}
for (i = 0; i < s->len; i++) {
c[i] = s->str[i];
}
return c;
}
void
anthy_free_xstr(xstr *x)
{
if (!x) {
return ;
}
/**/
free(x->str);
free(x);
}
void
anthy_free_xstr_str(xstr *x)
{
if (!x) {
return ;
}
free(x->str);
}
int
anthy_sputxchar(char *buf, xchar x, int encoding)
{
if (!xc_isprint(x)) {
sprintf(buf, "??");
return 2;
}
if (encoding == ANTHY_UTF8_ENCODING) {
return put_xchar_to_utf8_str(x, buf);
}
x = anthy_ucs_to_euc(x);
if (x < 256) {
buf[0] = x;
buf[1] = 0;
return 1;
}
buf[2] = 0;
buf[1] = 0x80 | (x & 255);
buf[0] = 0x80 | ((x>>8) & 255);
return 2;
}
int
anthy_sputxstr(char *buf, xstr *x, int encoding)
{
char b[MAX_BYTES_PER_XCHAR];
int i, l = 0;
for (i = 0; i < x->len; i++) {
anthy_sputxchar(b, x->str[i], encoding);
sprintf(&buf[l], "%s", b);
l += strlen(b);
}
return l;
}
int
anthy_snputxstr(char *buf, int n, xstr *x, int encoding)
{
char b[MAX_BYTES_PER_XCHAR];
int i, l=0;
for (i = 0; i < x->len; i++) {
anthy_sputxchar(b, x->str[i], encoding);
if ((int)strlen(b) + l >= n) {
return l;
}
n -= sprintf(&buf[l], "%s", b);
l += strlen(b);
}
return l;
}
void
anthy_putxchar(xchar x)
{
char buf[MAX_BYTES_PER_XCHAR];
if (!xc_isprint(x)) {
printf("\\%x", x);
return ;
}
anthy_sputxchar(buf, x, print_encoding);
printf("%s", buf);
}
void
anthy_putxstr(xstr *x)
{
int i;
for (i = 0; i < x->len; i++) {
anthy_putxchar(x->str[i]);
}
}
void
anthy_putxstrln(xstr *x)
{
anthy_putxstr(x);
printf("\n");
}
xstr*
anthy_xstrcpy(xstr *dest, xstr *src)
{
int i;
/* 文字列をコピー */
dest->len = src->len;
for (i = 0; i < src->len; i++) {
dest->str[i] = src->str[i];
}
return dest;
}
/* 返り値の符号はstrcmpと同じ */
int
anthy_xstrcmp(xstr *x1, xstr *x2)
{
int i, m;
if (x1->len < x2->len) {
m = x1->len;
}else{
m = x2->len;
}
for (i = 0 ; i < m ; i++) {
if (x1->str[i] < x2->str[i]) {
return -1;
}
if (x1->str[i] > x2->str[i]) {
return 1;
}
}
if (x1->len < x2->len) {
return -1;
}
if (x1->len > x2->len) {
return 1;
}
return 0;
}
/* 返り値の符号はstrncmpと同じ */
int
anthy_xstrncmp(xstr *x1, xstr *x2, int n)
{
int i, m;
if (x1->len < x2->len) {
m = x1->len;
}else{
m = x2->len;
}
if (m > n) m = n;
for (i = 0 ; i < m ; i++) {
if (x1->str[i] < x2->str[i]) {
return -1;
}
if (x1->str[i] > x2->str[i]) {
return 1;
}
}
if (x2->len <= n && x1->len < x2->len) {
return -1;
}
if (x1->len <= n && x1->len > x2->len) {
return 1;
}
return 0;
}
xstr *
anthy_xstrcat(xstr *s, xstr *a)
{
int i, l;
if (!s) {
s = malloc(sizeof(xstr));
s->str = NULL;
s->len = 0;
}
l = s->len + a->len;
if (l < 1) { /* 辞書もしくは学習データが壊れていた時の対策 */
free(s->str);
s->str = NULL;
s->len = 0;
return s;
}
s->str = realloc(s->str, sizeof(xchar)*l);
for (i = 0; i < a->len; i ++) {
s->str[s->len+i] = a->str[i];
}
s->len = l;
return s;
}
xstr *
anthy_xstrappend(xstr *xs, xchar xc)
{
xstr p;
xchar q[1];
p.len = 1;
p.str = q;
q[0] = xc;
return anthy_xstrcat(xs, &p);
}
long long
anthy_xstrtoll(xstr *x)
{
xchar c;
int i;
long long n = 0;/* 数 */
if (!x->len || x->len > 16) {
return -1;
}
if (!(anthy_get_xstr_type(x) & (XCT_NUM | XCT_WIDENUM))) {
return -1;
}
for (i = 0; i < x->len; i++) {
c = x->str[i];
n *= 10;
n += anthy_xchar_to_num(c);
}
return n;
}
/** 全角の数字を半角にする
*/
xstr *
anthy_xstr_wide_num_to_num(xstr* src_xs)
{
int i;
xstr *dst_xs;
dst_xs = anthy_xstr_dup(src_xs);
for (i = 0; i < src_xs->len; ++i) {
dst_xs->str[i] = anthy_xchar_wide_num_to_num(src_xs->str[i]);
}
return dst_xs;
}
/** 平仮名をカタカナに変換する
*/
xstr *
anthy_xstr_hira_to_kata(xstr *src_xs)
{
xstr *dst_xs;
int i, j;
dst_xs = anthy_xstr_dup(src_xs);
for (i = 0 ,j = 0; i < dst_xs->len; i++, j++) {
/* 「う゛」のチェック */
if (i < dst_xs->len - 1 && dst_xs->str[i] == HK_U
&& dst_xs->str[i+1] == HK_DDOT) {
dst_xs->str[j] = KK_VU;/* ヴ */
i++;
continue ;
}
/**/
dst_xs->str[j] = dst_xs->str[i];
if ((anthy_ucs_to_euc(dst_xs->str[j]) & 0xff00) == 0xa400) {
/* ひらがなだったら256足す */
dst_xs->str[j] = anthy_ucs_to_euc(dst_xs->str[j]);
dst_xs->str[j] += 256;
dst_xs->str[j] = anthy_euc_to_ucs(dst_xs->str[j]);
}
}
dst_xs->len = j;
return dst_xs;
}
xstr *
anthy_xstr_hira_to_half_kata(xstr *src_xs)
{
int len = src_xs->len;
int i, j;
xstr *xs;
for (i = 0; i < src_xs->len; i++) {
const struct half_kana_table *tab = anthy_find_half_kana(src_xs->str[i]);
if (tab && tab->mod) {
len ++;
}
}
xs = malloc(sizeof(xstr));
xs->len = len;
xs->str = malloc(sizeof(xchar) * len);
j = 0;
for (i = 0; i < src_xs->len; i++) {
const struct half_kana_table *tab = anthy_find_half_kana(src_xs->str[i]);
if (tab) {
xs->str[j] = anthy_euc_to_ucs(tab->dst);
if (tab->mod) {
j++;
xs->str[j] = anthy_euc_to_ucs(tab->mod);
}
} else {
xs->str[j] = src_xs->str[i];
}
j++;
}
return xs;
}
xstr *
anthy_conv_half_wide(xstr *xs)
{
int i;
xstr *res;
for (i = 0; i < xs->len; i++) {
if (!anthy_lookup_half_wide(xs->str[i])) {
return NULL;
}
}
res = anthy_xstr_dup(xs);
for (i = 0; i < xs->len; i++) {
res->str[i] = anthy_lookup_half_wide(xs->str[i]);
}
return res;
}
int
anthy_xstr_hash(xstr *xs)
{
int h,i;
h = 0;
for (i = 0 ;i < xs->len ;i++) {
h *= 97;
h += xs->str[i]<<4;
h += xs->str[i]>>4;
}
if (h < 0) {
return -h;
}
return h;
}
static char *
conv_cstr(const char *s, int from, int to)
{
char *res;
xstr *xs = anthy_cstr_to_xstr(s, from);
if (!xs) {
return NULL;
}
res = anthy_xstr_to_cstr(xs, to);
anthy_free_xstr(xs);
return res;
}
char *
anthy_conv_euc_to_utf8(const char *s)
{
return conv_cstr(s, ANTHY_EUC_JP_ENCODING, ANTHY_UTF8_ENCODING);
}
char *
anthy_conv_utf8_to_euc(const char *s)
{
return conv_cstr(s, ANTHY_UTF8_ENCODING, ANTHY_EUC_JP_ENCODING);
}
void
anthy_xstr_set_print_encoding(int encoding)
{
print_encoding = encoding;
}
int
anthy_init_xstr(void)
{
return 0;
}
void anthy_quit_xstr(void)
{
}
|