sj3/string2.c
2026-06-18 13:40:31 +09:00

176 lines
3.6 KiB
C

/*
* Copyright (c) 1991-1994 Sony Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include "const.h"
#include "dicttool.h"
int bubun_str(unsigned char* p1, int l1, unsigned char* p2, int l2) {
unsigned char* p;
int l;
int i;
for(p = p1, l = l1; l > 0;) {
if(l < l2) break;
for(i = 0; i < l2; i++)
if(*(p + i) != *(p2 + i)) break;
if(i >= l2) return 1;
i = codesize(*p);
p += i;
l -= i;
}
for(p = p2, l = l2; l > 0;) {
if(l < l1) break;
for(i = 0; i < l1; i++)
if(*(p + i) != *(p1 + i)) break;
if(i >= l1) return -1;
i = codesize(*p);
p += i;
l -= i;
}
return 0;
}
int overlap_str(unsigned char* p1, int l1, unsigned char* p2, int l2) {
unsigned char* p;
int l;
int i;
int j;
unsigned char tmp[MaxKanjiLength * 2 + MaxAtrNumber * 2];
for(p = p1, l = l1; l > 0;) {
if(l < l2) {
for(i = 0; (i < l) && (*(p + i) == *(p2 + i)); i++);
if((i >= l) && (l2 + l1 - l < sizeof(tmp))) {
for(i = 0; i < l1; i++)
tmp[i] = *(p1 + i);
for(j = l; j < l2; i++, j++)
tmp[i] = *(p2 + j);
if(check_hindo(tmp, i)) {
return -1;
}
}
}
i = codesize(*p);
p += i;
l -= i;
}
for(p = p2, l = l2; l > 0;) {
if(l < l1) {
for(i = 0; (i < l) && (*(p + i) == *(p1 + i)); i++);
if((i >= l) && (l1 + l2 - l < sizeof(tmp))) {
for(i = 0; i < l2; i++)
tmp[i] = *(p2 + i);
for(j = l; j < l1; i++, j++)
tmp[i] = *(p1 + j);
if(check_hindo(tmp, i)) {
return -1;
}
}
}
i = codesize(*p);
p += i;
l -= i;
}
return 0;
}
size_t
istrlen(int* s) {
int* p;
for(p = s; *p; p++);
return (p - s);
}
int istrcmp(int* s, int* d) {
for(; *s && (*s == *d); s++, d++);
return (*s - *d);
}
int top_strcmp(int* src, int* dst) {
int i = 0;
for(i = 0; *src && (*src == *dst); src++, dst++)
i++;
return (i > MaxYomiAskNumber) ? MaxYomiAskNumber : i;
}
int last_strcmp(int* src, int* dst) {
int slen, dlen;
slen = istrlen(src);
dlen = istrlen(dst);
if(dlen > MaxYomiAskNumber)
return 0;
if(istrcmp(src + (slen - dlen), dst))
return 0;
return dlen;
}
int string_cmp(unsigned char* p1, int l1, unsigned char* p2, int l2) {
while(l1 > 0 && l2 > 0) {
if(*p1 != *p2) return (*p1 - *p2);
p1++;
l1--;
p2++;
l2--;
}
if(l1 == l2) return 0;
if(l1 > l2) return 1;
return -1;
}