⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utf.c

📁 在x86平台上运行不可信任代码的sandbox。
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 1998-2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */#define _BSD_SOURCE 1 /* for memcccpy */#include <stdarg.h>#include <string.h>#include <stdlib.h>#include "u.h"#include "utf.h"#define bsearch utfbsearch/* * compiler directive on Plan 9 */#ifndef USED#define USED(x) if(x);else#endif/* * nil cannot be ((void*)0) on ANSI C, * because it is used for function pointers */#undef	nil#define	nil	0#undef	nelem#define	nelem(x)	(sizeof (x)/sizeof (x)[0])/* ---------- end preamble -------- *//* -------------- rune.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"enum{	Bit1	= 7,	Bitx	= 6,	Bit2	= 5,	Bit3	= 4,	Bit4	= 3,	T1	= ((1<<(Bit1+1))-1) ^ 0xFF,	/* 0000 0000 */	Tx	= ((1<<(Bitx+1))-1) ^ 0xFF,	/* 1000 0000 */	T2	= ((1<<(Bit2+1))-1) ^ 0xFF,	/* 1100 0000 */	T3	= ((1<<(Bit3+1))-1) ^ 0xFF,	/* 1110 0000 */	T4	= ((1<<(Bit4+1))-1) ^ 0xFF,	/* 1111 0000 */	Rune1	= (1<<(Bit1+0*Bitx))-1,		/* 0000 0000 0111 1111 */	Rune2	= (1<<(Bit2+1*Bitx))-1,		/* 0000 0111 1111 1111 */	Rune3	= (1<<(Bit3+2*Bitx))-1,		/* 1111 1111 1111 1111 */	Maskx	= (1<<Bitx)-1,			/* 0011 1111 */	Testx	= Maskx ^ 0xFF,			/* 1100 0000 */	Bad	= Runeerror};intchartorune(Rune *rune, char *str){	int c, c1, c2;	long l;	/*	 * one character sequence	 *	00000-0007F => T1	 */	c = *(uchar*)str;	if(c < Tx) {		*rune = c;		return 1;	}	/*	 * two character sequence	 *	0080-07FF => T2 Tx	 */	c1 = *(uchar*)(str+1) ^ Tx;	if(c1 & Testx)		goto bad;	if(c < T3) {		if(c < T2)			goto bad;		l = ((c << Bitx) | c1) & Rune2;		if(l <= Rune1)			goto bad;		*rune = l;		return 2;	}	/*	 * three character sequence	 *	0800-FFFF => T3 Tx Tx	 */	c2 = *(uchar*)(str+2) ^ Tx;	if(c2 & Testx)		goto bad;	if(c < T4) {		l = ((((c << Bitx) | c1) << Bitx) | c2) & Rune3;		if(l <= Rune2)			goto bad;		*rune = l;		return 3;	}	/*	 * bad decoding	 */bad:	*rune = Bad;	return 1;}intrunetochar(char *str, Rune *rune){	long c;	/*	 * one character sequence	 *	00000-0007F => 00-7F	 */	c = *rune;	if(c <= Rune1) {		str[0] = c;		return 1;	}	/*	 * two character sequence	 *	0080-07FF => T2 Tx	 */	if(c <= Rune2) {		str[0] = T2 | (c >> 1*Bitx);		str[1] = Tx | (c & Maskx);		return 2;	}	/*	 * three character sequence	 *	0800-FFFF => T3 Tx Tx	 */	str[0] = T3 |  (c >> 2*Bitx);	str[1] = Tx | ((c >> 1*Bitx) & Maskx);	str[2] = Tx |  (c & Maskx);	return 3;}intrunelen(long c){	Rune rune;	char str[10];	rune = c;	return runetochar(str, &rune);}intrunenlen(Rune *r, int nrune){	int nb, c;	nb = 0;	while(nrune--) {		c = *r++;		if(c <= Rune1)			nb++;		else		if(c <= Rune2)			nb += 2;		else			nb += 3;	}	return nb;}intfullrune(char *str, int n){	int c;	if(n > 0) {		c = *(uchar*)str;		if(c < Tx)			return 1;		if(n > 1)			if(c < T3 || n > 2)				return 1;	}	return 0;}/* -------------- runestrcat.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"Rune*runestrcat(Rune *s1, Rune *s2){	runestrcpy(runestrchr(s1, 0), s2);	return s1;}/* -------------- runestrchr.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"Rune*runestrchr(Rune *s, Rune c){	Rune c0 = c;	Rune c1;	if(c == 0) {		while(*s++)			;		return s-1;	}	while((c1 = *s++))		if(c1 == c0)			return s-1;	return 0;}/* -------------- runestrcmp.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"intrunestrcmp(Rune *s1, Rune *s2){	Rune c1, c2;	for(;;) {		c1 = *s1++;		c2 = *s2++;		if(c1 != c2) {			if(c1 > c2)				return 1;			return -1;		}		if(c1 == 0)			return 0;	}}/* -------------- runestrcpy.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"Rune*runestrcpy(Rune *s1, Rune *s2){	Rune *os1;	os1 = s1;	while((*s1++ = *s2++))		;	return os1;}/* -------------- runestrdup.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include <stdlib.h>// #include "plan9.h"// #include "utf.h"Rune*runestrdup(Rune *s) {  	Rune *ns;	ns = malloc(sizeof(Rune)*(runestrlen(s) + 1));	if(ns == 0)		return 0;	return runestrcpy(ns, s);}/* -------------- runestrlen.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"longrunestrlen(Rune *s){	return runestrchr(s, 0) - s;}/* -------------- runestrecpy.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"Rune*runestrecpy(Rune *s1, Rune *es1, Rune *s2){	if(s1 >= es1)		return s1;	while((*s1++ = *s2++)){		if(s1 == es1){			*--s1 = '\0';			break;		}	}	return s1;}/* -------------- runestrncat.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"Rune*runestrncat(Rune *s1, Rune *s2, long n){	Rune *os1;	os1 = s1;	s1 = runestrchr(s1, 0);	while((*s1++ = *s2++))		if(--n < 0) {			s1[-1] = 0;			break;		}	return os1;}/* -------------- runestrncmp.c --------------- *//* * The authors of this software are Rob Pike and Ken Thompson. *              Copyright (c) 2002 by Lucent Technologies. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */// #include <stdarg.h>// #include <string.h>// #include "plan9.h"// #include "utf.h"intrunestrncmp(Rune *s1, Rune *s2, long n){	Rune c1, c2;	while(n > 0) {		c1 = *s1++;		c2 = *s2++;		n--;		if(c1 != c2) {			if(c1 > c2)				return 1;			return -1;		}		if(c1 == 0)			break;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -