PDA

View Full Version : سوال: سوال : تشخیص کاراکترهای یک رشته در utf8



omidd1315
شنبه 09 فروردین 1393, 10:37 صبح
با سلام

من آرایه کاراکتری زیر را دارم که شامل کاراکترهای utf8 می باشد.

char* a;

به دلیل اینکه سایز هر کاراکتر در utf8 بین یک تا چهار بایت متغیره تشخیص کد مربوط به کاراکتر در آرایه سخته.
آیا راهی هست که بتونیم کاراکترها را با هر سایزی تک تک از این رشته جدا کنیم ؟
در واقع من مخوام که یک کاراکتر وسطش اضافه کنم ولی با سایز متغیری که هست نمیشه تشخیص داد کجا یک کاراکتر تموم میشه.

ممنون

rahnema1
شنبه 09 فروردین 1393, 17:16 عصر
سلام
این نحوه تبدیل utf8 به ucs4 و برعکسش هست که از کتابخانه iconv گرفتم نحوه استفاده هم توی تابه main گذاشتم

#include <stdio.h>
typedef unsigned int ucs4_t;
/* Return code if invalid input after a shift sequence of n bytes was read.
(xxx_mbtowc) */
#define RET_SHIFT_ILSEQ(n) (-1-2*(n))
/* Return code if invalid. (xxx_mbtowc) */
#define RET_ILSEQ RET_SHIFT_ILSEQ(0)
/* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
#define RET_TOOFEW(n) (-2-2*(n))
/* Retrieve the n from the encoded RET_... value. */
#define DECODE_SHIFT_ILSEQ(r) ((unsigned int)(RET_SHIFT_ILSEQ(0) - (r)) / 2)
#define DECODE_TOOFEW(r) ((unsigned int)(RET_TOOFEW(0) - (r)) / 2)
/* Return code if invalid. (xxx_wctomb) */
#define RET_ILUNI -1
/* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
#define RET_TOOSMALL -2
static int
utf8_mbtowc ( ucs4_t *pwc, const unsigned char *s, int n)
{
unsigned char c = s[0];

if (c < 0x80) {
*pwc = c;
return 1;
} else if (c < 0xc2) {
return RET_ILSEQ;
} else if (c < 0xe0) {
if (n < 2)
return RET_TOOFEW(0);
if (!((s[1] ^ 0x80) < 0x40))
return RET_ILSEQ;
*pwc = ((ucs4_t) (c & 0x1f) << 6)
| (ucs4_t) (s[1] ^ 0x80);
return 2;
} else if (c < 0xf0) {
if (n < 3)
return RET_TOOFEW(0);
if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
&& (c >= 0xe1 || s[1] >= 0xa0)))
return RET_ILSEQ;
*pwc = ((ucs4_t) (c & 0x0f) << 12)
| ((ucs4_t) (s[1] ^ 0x80) << 6)
| (ucs4_t) (s[2] ^ 0x80);
return 3;
} else if (c < 0xf8 && sizeof(ucs4_t)*8 >= 32) {
if (n < 4)
return RET_TOOFEW(0);
if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
&& (s[3] ^ 0x80) < 0x40
&& (c >= 0xf1 || s[1] >= 0x90)))
return RET_ILSEQ;
*pwc = ((ucs4_t) (c & 0x07) << 18)
| ((ucs4_t) (s[1] ^ 0x80) << 12)
| ((ucs4_t) (s[2] ^ 0x80) << 6)
| (ucs4_t) (s[3] ^ 0x80);
return 4;
} else if (c < 0xfc && sizeof(ucs4_t)*8 >= 32) {
if (n < 5)
return RET_TOOFEW(0);
if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
&& (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
&& (c >= 0xf9 || s[1] >= 0x88)))
return RET_ILSEQ;
*pwc = ((ucs4_t) (c & 0x03) << 24)
| ((ucs4_t) (s[1] ^ 0x80) << 18)
| ((ucs4_t) (s[2] ^ 0x80) << 12)
| ((ucs4_t) (s[3] ^ 0x80) << 6)
| (ucs4_t) (s[4] ^ 0x80);
return 5;
} else if (c < 0xfe && sizeof(ucs4_t)*8 >= 32) {
if (n < 6)
return RET_TOOFEW(0);
if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
&& (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
&& (s[5] ^ 0x80) < 0x40
&& (c >= 0xfd || s[1] >= 0x84)))
return RET_ILSEQ;
*pwc = ((ucs4_t) (c & 0x01) << 30)
| ((ucs4_t) (s[1] ^ 0x80) << 24)
| ((ucs4_t) (s[2] ^ 0x80) << 18)
| ((ucs4_t) (s[3] ^ 0x80) << 12)
| ((ucs4_t) (s[4] ^ 0x80) << 6)
| (ucs4_t) (s[5] ^ 0x80);
return 6;
} else
return RET_ILSEQ;
}

static int
utf8_wctomb ( unsigned char *r, ucs4_t wc, int n) /* n == 0 is acceptable */
{
int count;
if (wc < 0x80)
count = 1;
else if (wc < 0x800)
count = 2;
else if (wc < 0x10000)
count = 3;
else if (wc < 0x200000)
count = 4;
else if (wc < 0x4000000)
count = 5;
else if (wc <= 0x7fffffff)
count = 6;
else
return RET_ILUNI;
if (n < count)
return RET_TOOSMALL;
switch (count) { /* note: code falls through cases! */
case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
case 1: r[0] = wc;
}
return count;
}
int main(){
const unsigned char a[]="ﻰﺳﺭﺎﻓ";
ucs4_t b=0;
int ss=0,ii=0;
while(ss<sizeof a-1)
{
ii=utf8_mbtowc(&b,a+(ss+=ii),4);
printf("%lu\n",b);
}
}