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

📄 varlena.c

📁 关系型数据库 Postgresql 6.5.2
💻 C
📖 第 1 页 / 共 2 页
字号:
	ps1 = p1 = (pg_wchar *) palloc((len1 + 1) * sizeof(pg_wchar));	(void) pg_mb2wchar_with_len((unsigned char *) VARDATA(t1), p1, len1);	len1 = pg_wchar_strlen(p1);	ps2 = p2 = (pg_wchar *) palloc((len2 + 1) * sizeof(pg_wchar));	(void) pg_mb2wchar_with_len((unsigned char *) VARDATA(t2), p2, len2);	len2 = pg_wchar_strlen(p2);#else	p1 = VARDATA(t1);	p2 = VARDATA(t2);#endif	pos = 0;	px = (len1 - len2);	for (p = 0; p <= px; p++)	{#ifdef MULTIBYTE		if ((*p2 == *p1) && (pg_wchar_strncmp(p1, p2, len2) == 0))#else		if ((*p2 == *p1) && (strncmp(p1, p2, len2) == 0))#endif		{			pos = p + 1;			break;		};		p1++;	};#ifdef MULTIBYTE	pfree(ps1);	pfree(ps2);#endif	return pos;}	/* textpos() *//* *		texteq			- returns 1 iff arguments are equal *		textne			- returns 1 iff arguments are not equal */booltexteq(text *arg1, text *arg2){	int			len;	char	   *a1p,			   *a2p;	if (arg1 == NULL || arg2 == NULL)		return (bool) NULL;	if ((len = arg1->vl_len) != arg2->vl_len)		return (bool) 0;	a1p = arg1->vl_dat;	a2p = arg2->vl_dat;	/*	 * Varlenas are stored as the total size (data + size variable)	 * followed by the data. Use VARHDRSZ instead of explicit sizeof() -	 * thomas 1997-07-10	 */	len -= VARHDRSZ;	while (len-- != 0)		if (*a1p++ != *a2p++)			return (bool) 0;	return (bool) 1;}	/* texteq() */booltextne(text *arg1, text *arg2){	return (bool) !texteq(arg1, arg2);}/* varstr_cmp() * Comparison function for text strings with given lengths. * Includes locale support, but must copy strings to temporary memory *	to allow null-termination for inputs to strcoll(). * Returns -1, 0 or 1 */intvarstr_cmp(char *arg1, int len1, char *arg2, int len2){	int			result;	char	   *a1p,			   *a2p;#ifdef USE_LOCALE	a1p = (unsigned char *) palloc(len1 + 1);	a2p = (unsigned char *) palloc(len2 + 1);	memcpy(a1p, arg1, len1);	*(a1p + len1) = '\0';	memcpy(a2p, arg2, len2);	*(a2p + len2) = '\0';	result = strcoll(a1p, a2p);	pfree(a1p);	pfree(a2p);#else	a1p = arg1;	a2p = arg2;	result = strncmp(a1p, a2p, Min(len1, len2));	if ((result == 0) && (len1 != len2))		result = (len1 < len2) ? -1 : 1;#endif	return result;}	/* varstr_cmp() *//* text_cmp() * Comparison function for text strings. * Includes locale support, but must copy strings to temporary memory *	to allow null-termination for inputs to strcoll(). * XXX HACK code for textlen() indicates that there can be embedded nulls *	but it appears that most routines (incl. this one) assume not! - tgl 97/04/07 * Returns -1, 0 or 1 */static inttext_cmp(text *arg1, text *arg2){	char	   *a1p,			   *a2p;	int			len1,				len2;	if (arg1 == NULL || arg2 == NULL)		return (bool) FALSE;	a1p = VARDATA(arg1);	a2p = VARDATA(arg2);	len1 = VARSIZE(arg1) - VARHDRSZ;	len2 = VARSIZE(arg2) - VARHDRSZ;	return varstr_cmp(a1p, len1, a2p, len2);}	/* text_cmp() *//* text_lt() * Comparison function for text strings. */booltext_lt(text *arg1, text *arg2){	return (bool) (text_cmp(arg1, arg2) < 0);}	/* text_lt() *//* text_le() * Comparison function for text strings. */booltext_le(text *arg1, text *arg2){	return (bool) (text_cmp(arg1, arg2) <= 0);}	/* text_le() */booltext_gt(text *arg1, text *arg2){	return (bool) !text_le(arg1, arg2);}booltext_ge(text *arg1, text *arg2){	return (bool) !text_lt(arg1, arg2);}text *text_larger(text *arg1, text *arg2){	text	   *result;	text	   *temp;	temp = ((text_cmp(arg1, arg2) <= 0) ? arg2 : arg1);	/* Make a copy */	result = (text *) palloc(VARSIZE(temp));	memmove((char *) result, (char *) temp, VARSIZE(temp));	return (result);}text *text_smaller(text *arg1, text *arg2){	text	   *result;	text	   *temp;	temp = ((text_cmp(arg1, arg2) > 0) ? arg2 : arg1);	/* Make a copy */	result = (text *) palloc(VARSIZE(temp));	memmove((char *) result, (char *) temp, VARSIZE(temp));	return (result);}/*------------------------------------------------------------- * byteaGetSize * * get the number of bytes contained in an instance of type 'bytea' *------------------------------------------------------------- */int32byteaGetSize(text *v){	int			len;	len = v->vl_len - sizeof(v->vl_len);	return len;}/*------------------------------------------------------------- * byteaGetByte * * this routine treats "bytea" as an array of bytes. * It returns the Nth byte (a number between 0 and 255) or * it dies if the length of this array is less than n. *------------------------------------------------------------- */int32byteaGetByte(text *v, int32 n){	int			len;	int			byte;	len = byteaGetSize(v);	if (n >= len)	{		elog(ERROR, "byteaGetByte: index (=%d) out of range [0..%d]",			 n, len - 1);	}#ifdef USE_LOCALE	byte = (unsigned char) (v->vl_dat[n]);#else	byte = v->vl_dat[n];#endif	return (int32) byte;}/*------------------------------------------------------------- * byteaGetBit * * This routine treats a "bytea" type like an array of bits. * It returns the value of the Nth bit (0 or 1). * If 'n' is out of range, it dies! * *------------------------------------------------------------- */int32byteaGetBit(text *v, int32 n){	int			byteNo,				bitNo;	int			byte;	byteNo = n / 8;	bitNo = n % 8;	byte = byteaGetByte(v, byteNo);	if (byte & (1 << bitNo))		return (int32) 1;	else		return (int32) 0;}/*------------------------------------------------------------- * byteaSetByte * * Given an instance of type 'bytea' creates a new one with * the Nth byte set to the given value. * *------------------------------------------------------------- */text *byteaSetByte(text *v, int32 n, int32 newByte){	int			len;	text	   *res;	len = byteaGetSize(v);	if (n >= len)	{		elog(ERROR,			 "byteaSetByte: index (=%d) out of range [0..%d]",			 n, len - 1);	}	/*	 * Make a copy of the original varlena.	 */	res = (text *) palloc(VARSIZE(v));	if (res == NULL)	{		elog(ERROR, "byteaSetByte: Out of memory (%d bytes requested)",			 VARSIZE(v));	}	memmove((char *) res, (char *) v, VARSIZE(v));	/*	 * Now set the byte.	 */	res->vl_dat[n] = newByte;	return res;}/*------------------------------------------------------------- * byteaSetBit * * Given an instance of type 'bytea' creates a new one with * the Nth bit set to the given value. * *------------------------------------------------------------- */text *byteaSetBit(text *v, int32 n, int32 newBit){	text	   *res;	int			oldByte,				newByte;	int			byteNo,				bitNo;	/*	 * sanity check!	 */	if (newBit != 0 && newBit != 1)		elog(ERROR, "byteaSetByte: new bit must be 0 or 1");	/*	 * get the byte where the bit we want is stored.	 */	byteNo = n / 8;	bitNo = n % 8;	oldByte = byteaGetByte(v, byteNo);	/*	 * calculate the new value for that byte	 */	if (newBit == 0)		newByte = oldByte & (~(1 << bitNo));	else		newByte = oldByte | (1 << bitNo);	/*	 * NOTE: 'byteaSetByte' creates a copy of 'v' & sets the byte.	 */	res = byteaSetByte(v, byteNo, newByte);	return res;}/* text_name() * Converts a text() type to a NameData type. */NameData   *text_name(text *s){	NameData   *result;	int			len;	if (s == NULL)		return NULL;	len = VARSIZE(s) - VARHDRSZ;	if (len > NAMEDATALEN)		len = NAMEDATALEN;#ifdef STRINGDEBUG	printf("text- convert string length %d (%d) ->%d\n",		   VARSIZE(s) - VARHDRSZ, VARSIZE(s), len);#endif	result = palloc(NAMEDATALEN);	StrNCpy(result->data, VARDATA(s), NAMEDATALEN);	/* now null pad to full length... */	while (len < NAMEDATALEN)	{		*(result->data + len) = '\0';		len++;	}	return result;}	/* text_name() *//* name_text() * Converts a NameData type to a text type. */text *name_text(NameData *s){	text	   *result;	int			len;	if (s == NULL)		return NULL;	len = strlen(s->data);#ifdef STRINGDEBUG	printf("text- convert string length %d (%d) ->%d\n",		   VARSIZE(s) - VARHDRSZ, VARSIZE(s), len);#endif	result = palloc(VARHDRSZ + len);	strncpy(VARDATA(result), s->data, len);	VARSIZE(result) = len + VARHDRSZ;	return result;}	/* name_text() */

⌨️ 快捷键说明

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