瀏覽代碼

libdpkg: Add new pure ASCII C locale character type functions

These do not get affected by the user locale, nor by the character
encodings. For dpkg control data, we always want to use ASCII.
Guillem Jover 11 年之前
父節點
當前提交
8183122e10
共有 8 個文件被更改,包括 429 次插入0 次删除
  1. 2 0
      lib/dpkg/Makefile.am
  2. 186 0
      lib/dpkg/c-ctype.c
  3. 130 0
      lib/dpkg/c-ctype.h
  4. 1 0
      lib/dpkg/libdpkg.map
  5. 1 0
      lib/dpkg/test/.gitignore
  6. 1 0
      lib/dpkg/test/Makefile.am
  7. 107 0
      lib/dpkg/test/t-c-ctype.c
  8. 1 0
      po/POTFILES.in

+ 2 - 0
lib/dpkg/Makefile.am

@@ -44,6 +44,7 @@ libdpkg_la_SOURCES = \
 	arch.c \
 	atomic-file.c \
 	buffer.c \
+	c-ctype.c \
 	cleanup.c \
 	command.c \
 	compress.c \
@@ -102,6 +103,7 @@ pkginclude_HEADERS = \
 	arch.h \
 	atomic-file.h \
 	buffer.h \
+	c-ctype.h \
 	command.h \
 	compress.h \
 	deb-version.h \

+ 186 - 0
lib/dpkg/c-ctype.c

@@ -0,0 +1,186 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * c-ctype.c - ASCII C locale-only functions
+ *
+ * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg/c-ctype.h>
+
+#undef S
+#define S C_CTYPE_SPACE
+#undef W
+#define W C_CTYPE_WHITE
+#undef B
+#define B C_CTYPE_BLANK
+#undef U
+#define U C_CTYPE_UPPER
+#undef L
+#define L C_CTYPE_LOWER
+#undef D
+#define D C_CTYPE_DIGIT
+
+static unsigned short int c_ctype[256] = {
+/** 0 **/
+	/* \0 */ 0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	/* \a */ 0,
+	/* \b */ 0,
+	/* \t */ S | W | B,
+	/* \n */ S | W,
+	/* \v */ S,
+	/* \f */ S,
+	/* \r */ S,
+	0,
+	0,
+/** 16 **/
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+	0,
+/** 32 **/
+	/*   */ S | W | B,
+	/* ! */ 0,
+	/* " */ 0,
+	/* # */ 0,
+	/* $ */ 0,
+	/* % */ 0,
+	/* & */ 0,
+	/* ' */ 0,
+	/* ( */ 0,
+	/* ) */ 0,
+	/* * */ 0,
+	/* + */ 0,
+	/* , */ 0,
+	/* - */ 0,
+	/* . */ 0,
+	/* / */ 0,
+/** 48 **/
+	/* 0 */ D,
+	/* 1 */ D,
+	/* 2 */ D,
+	/* 3 */ D,
+	/* 4 */ D,
+	/* 5 */ D,
+	/* 6 */ D,
+	/* 7 */ D,
+	/* 8 */ D,
+	/* 9 */ D,
+	/* : */ 0,
+	/* ; */ 0,
+	/* < */ 0,
+	/* = */ 0,
+	/* > */ 0,
+	/* ? */ 0,
+/* 64 */
+	/* @ */ 0,
+	/* A */ U,
+	/* B */ U,
+	/* C */ U,
+	/* D */ U,
+	/* E */ U,
+	/* F */ U,
+	/* G */ U,
+	/* H */ U,
+	/* I */ U,
+	/* J */ U,
+	/* K */ U,
+	/* L */ U,
+	/* M */ U,
+	/* N */ U,
+	/* O */ U,
+/* 80 */
+	/* P */ U,
+	/* Q */ U,
+	/* R */ U,
+	/* S */ U,
+	/* T */ U,
+	/* U */ U,
+	/* V */ U,
+	/* W */ U,
+	/* X */ U,
+	/* Y */ U,
+	/* Z */ U,
+	/* [ */ 0,
+	/* \ */ 0,
+	/* ] */ 0,
+	/* ^ */ 0,
+	/* _ */ 0,
+/* 96 */
+	/* ` */ 0,
+	/* a */ L,
+	/* b */ L,
+	/* c */ L,
+	/* d */ L,
+	/* e */ L,
+	/* f */ L,
+	/* g */ L,
+	/* h */ L,
+	/* i */ L,
+	/* j */ L,
+	/* k */ L,
+	/* l */ L,
+	/* m */ L,
+	/* n */ L,
+	/* o */ L,
+/* 112 */
+	/* p */ L,
+	/* q */ L,
+	/* r */ L,
+	/* s */ L,
+	/* t */ L,
+	/* u */ L,
+	/* v */ L,
+	/* w */ L,
+	/* x */ L,
+	/* y */ L,
+	/* z */ L,
+	/* { */ 0,
+	/* | */ 0,
+	/* } */ 0,
+	/* ~ */ 0,
+	0,
+/* 128 */
+};
+
+/**
+ * Check if the character is bits ctype.
+ */
+bool
+c_isbits(int c, enum c_ctype_bit bits)
+{
+	return ((c_ctype[c] & bits) != 0);
+}

+ 130 - 0
lib/dpkg/c-ctype.h

@@ -0,0 +1,130 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * c-ctype.h - ASCII C locale-only functions
+ *
+ * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBDPKG_C_CTYPE_H
+#define LIBDPKG_C_CTYPE_H
+
+#include <stdbool.h>
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+#define C_CTYPE_BIT(bit)	(1 << (bit))
+
+enum c_ctype_bit {
+	C_CTYPE_BLANK = C_CTYPE_BIT(0),
+	C_CTYPE_WHITE = C_CTYPE_BIT(1),
+	C_CTYPE_SPACE = C_CTYPE_BIT(2),
+	C_CTYPE_UPPER = C_CTYPE_BIT(3),
+	C_CTYPE_LOWER = C_CTYPE_BIT(4),
+	C_CTYPE_DIGIT = C_CTYPE_BIT(5),
+
+	C_CTYPE_ALPHA = C_CTYPE_UPPER | C_CTYPE_LOWER,
+	C_CTYPE_ALNUM = C_CTYPE_ALPHA | C_CTYPE_DIGIT,
+};
+
+bool
+c_isbits(int c, enum c_ctype_bit bits);
+
+/**
+ * Check if the character is [ \t].
+ */
+static inline bool
+c_isblank(int c)
+{
+	return c_isbits(c, C_CTYPE_BLANK);
+}
+
+/**
+ * Check if the character is [ \t\n].
+ */
+static inline bool
+c_iswhite(int c)
+{
+	return c_isbits(c, C_CTYPE_WHITE);
+}
+
+/**
+ * Check if the character is [ \v\t\f\r\n].
+ */
+static inline bool
+c_isspace(int c)
+{
+	return c_isbits(c, C_CTYPE_SPACE);
+}
+
+/**
+ * Check if the character is [0-9].
+ */
+static inline bool
+c_isdigit(int c)
+{
+	return c_isbits(c, C_CTYPE_DIGIT);
+}
+
+/**
+ * Check if the character is [A-Z].
+ */
+static inline bool
+c_isupper(int c)
+{
+	return c_isbits(c, C_CTYPE_UPPER);
+}
+
+/**
+ * Check if the character is [a-z].
+ */
+static inline bool
+c_islower(int c)
+{
+	return c_isbits(c, C_CTYPE_LOWER);
+}
+
+/**
+ * Check if the character is [a-zA-Z].
+ */
+static inline bool
+c_isalpha(int c)
+{
+	return c_isbits(c, C_CTYPE_ALPHA);
+}
+
+/**
+ * Check if the character is [a-zA-Z0-9].
+ */
+static inline bool
+c_isalnum(int c)
+{
+	return c_isbits(c, C_CTYPE_ALNUM);
+}
+
+/**
+ * Maps the character to its lower-case form.
+ */
+static inline int
+c_tolower(int c)
+{
+	return (c_isupper(c) ? ((unsigned char)c & ~0x20) | 0x20 : c);
+}
+
+DPKG_END_DECLS
+
+#endif

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -70,6 +70,7 @@ LIBDPKG_PRIVATE {
 	fgets_checked;
 
 	# Charset and string functions
+	c_isbits;
 	cisdigit;
 
 	str_match_end;

+ 1 - 0
lib/dpkg/test/.gitignore

@@ -1,6 +1,7 @@
 t-ar
 t-arch
 t-buffer
+t-c-ctype
 t-command
 t-deb-version
 t-error

+ 1 - 0
lib/dpkg/test/Makefile.am

@@ -19,6 +19,7 @@ test_programs = \
 	t-test \
 	t-test-skip \
 	t-macros \
+	t-c-ctype \
 	t-error \
 	t-string \
 	t-buffer \

+ 107 - 0
lib/dpkg/test/t-c-ctype.c

@@ -0,0 +1,107 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-c-ctype.c - test C locale ctype functions
+ *
+ * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg/test.h>
+#include <dpkg/c-ctype.h>
+
+static void
+test_ctype(void)
+{
+	int c;
+
+	for (c = -1; c < 256; c++) {
+		/* Test blank. */
+		if (c == '\t' || c == ' ')
+			test_pass(c_isblank(c));
+		else
+			test_fail(c_isblank(c));
+
+		/* Test white. */
+		if (c == '\t' || c == ' ' || c == '\n')
+			test_pass(c_iswhite(c));
+		else
+			test_fail(c_iswhite(c));
+
+		/* Test space. */
+		if (c == '\t' || c == '\v' || c == '\f' ||
+		    c == '\r' || c == '\n' || c == ' ')
+			test_pass(c_isspace(c));
+		else
+			test_fail(c_isspace(c));
+
+		/* Test digit. */
+		if (c >= '0' && c <= '9')
+			test_pass(c_isdigit(c));
+		else
+			test_fail(c_isdigit(c));
+
+		/* Test lower case. */
+		if (c >= 'a' && c <= 'z')
+			test_pass(c_islower(c));
+		else
+			test_fail(c_islower(c));
+
+		/* Test upper case. */
+		if (c >= 'A' && c <= 'Z')
+			test_pass(c_isupper(c));
+		else
+			test_fail(c_isupper(c));
+
+		/* Test alpha. */
+		if (c_islower(c) || c_isupper(c))
+			test_pass(c_isalpha(c));
+		else
+			test_fail(c_isalpha(c));
+
+		/* Test alphanumeric. */
+		if (c_isdigit(c) || c_isalpha(c))
+			test_pass(c_isalnum(c));
+		else
+			test_fail(c_isalnum(c));
+	}
+}
+
+static void
+test_casing(void)
+{
+	test_pass(c_tolower('A') == 'a');
+	test_pass(c_tolower('Z') == 'z');
+
+	test_pass(c_tolower('a') == 'a');
+	test_pass(c_tolower('z') == 'z');
+
+	test_pass(c_tolower('0') == '0');
+	test_pass(c_tolower('9') == '9');
+
+	/* Test if we can handle the value for EOF. */
+	test_pass(c_tolower(-1) == -1);
+}
+
+static void
+test(void)
+{
+	test_plan(2063);
+
+	test_ctype();
+	test_casing();
+}

+ 1 - 0
po/POTFILES.in

@@ -4,6 +4,7 @@ lib/dpkg/ar.c
 lib/dpkg/arch.c
 lib/dpkg/atomic-file.c
 lib/dpkg/buffer.c
+lib/dpkg/c-ctype.c
 lib/dpkg/cleanup.c
 lib/dpkg/command.c
 lib/dpkg/compress.c