Skip to content
Snippets Groups Projects
int.c 29.2 KiB
Newer Older
/*-------------------------------------------------------------------------
 *
 *	  Functions for the built-in integer types (except int8).
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
Bruce Momjian's avatar
Bruce Momjian committed
 * Portions Copyright (c) 1994, Regents of the University of California
 *	  src/backend/utils/adt/int.c
 *
 *-------------------------------------------------------------------------
 */
/*
 * OLD COMMENTS
 *		 int2in, int2out, int2recv, int2send
 *		 int4in, int4out, int4recv, int4send
 *		 int2vectorin, int2vectorout, int2vectorrecv, int2vectorsend
 *		Boolean operators:
 *		 inteq, intne, intlt, intle, intgt, intge
 *		Arithmetic operators:
 *		 intpl, intmi, int4mul, intdiv
 *		Arithmetic operators:
Bruce Momjian's avatar
Bruce Momjian committed
#include "postgres.h"
#include "catalog/pg_type.h"
Bruce Momjian's avatar
Bruce Momjian committed
#include "utils/builtins.h"

#define SAMESIGN(a,b)	(((a) < 0) == ((b) < 0))

#define Int2VectorSize(n)	(offsetof(int2vector, values) + (n) * sizeof(int2))

typedef struct
{
	int32		current;
	int32		finish;
	int32		step;
Bruce Momjian's avatar
Bruce Momjian committed
} generate_series_fctx;
/*****************************************************************************
 *	 USER I/O ROUTINES														 *
 *****************************************************************************/

/*
 *		int2in			- converts "num" to short
	char	   *num = PG_GETARG_CSTRING(0);

	PG_RETURN_INT16(pg_atoi(num, sizeof(int16), '\0'));
 *		int2out			- converts short to "num"
Datum
int2out(PG_FUNCTION_ARGS)
	int16		arg1 = PG_GETARG_INT16(0);
	char	   *result = (char *) palloc(7);	/* sign, 5 digits, '\0' */
/*
 *		int2recv			- converts external binary format to int2
 */
Datum
int2recv(PG_FUNCTION_ARGS)
{
	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);

	PG_RETURN_INT16((int16) pq_getmsgint(buf, sizeof(int16)));
}

/*
 *		int2send			- converts int2 to binary format
 */
Datum
int2send(PG_FUNCTION_ARGS)
{
	int16		arg1 = PG_GETARG_INT16(0);
	StringInfoData buf;

	pq_begintypsend(&buf);
	pq_sendint(&buf, arg1, sizeof(int16));
	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

 * construct int2vector given a raw array of int2s
 * If int2s is NULL then caller must fill values[] afterward
 */
int2vector *
buildint2vector(const int2 *int2s, int n)
{
	int2vector *result;

	result = (int2vector *) palloc0(Int2VectorSize(n));

	if (n > 0 && int2s)
		memcpy(result->values, int2s, n * sizeof(int2));

	/*
	 * Attach standard array header.  For historical reasons, we set the index
	 * lower bound to 0 not 1.
	SET_VARSIZE(result, Int2VectorSize(n));
	result->dataoffset = 0;		/* never any nulls */
	result->elemtype = INT2OID;
	result->dim1 = n;
	result->lbound1 = 0;

	return result;
}

/*
 *		int2vectorin			- converts "num num ..." to internal form
Datum
int2vectorin(PG_FUNCTION_ARGS)
	char	   *intString = PG_GETARG_CSTRING(0);
	int2vector *result;
	int			n;

	result = (int2vector *) palloc0(Int2VectorSize(FUNC_MAX_ARGS));
	for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
		while (*intString && isspace((unsigned char) *intString))
		if (*intString == '\0')
Bruce Momjian's avatar
Bruce Momjian committed
			break;
		result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
		while (*intString && !isspace((unsigned char) *intString))
			intString++;
	while (*intString && isspace((unsigned char) *intString))
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("int2vector has too many elements")));

	SET_VARSIZE(result, Int2VectorSize(n));
	result->dataoffset = 0;		/* never any nulls */
	result->elemtype = INT2OID;
	result->dim1 = n;
	result->lbound1 = 0;
 *		int2vectorout		- converts internal form to "num num ..."
Datum
int2vectorout(PG_FUNCTION_ARGS)
	int2vector *int2Array = (int2vector *) PG_GETARG_POINTER(0);
	rp = result = (char *) palloc(nnums * 7 + 1);
	for (num = 0; num < nnums; num++)
		pg_itoa(int2Array->values[num], rp);
/*
 *		int2vectorrecv			- converts external binary format to int2vector
Loading
Loading full blame...