summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Jennings <mej@kainx.org>2006-04-25 15:18:41 +0000
committerMichael Jennings <mej@kainx.org>2006-04-25 15:18:41 +0000
commitf6fc5e71127f6648bcc4106eea6ecfe40dadcd16 (patch)
treecacb246ac1fe6ec13ec6291bfeea12c6a2f295f4
parentce56ce20df358e1b1718d79debca5a62d875b7d3 (diff)
downloadlibast-f6fc5e71127f6648bcc4106eea6ecfe40dadcd16.tar.gz
Tue Apr 25 10:19:10 2006 Michael Jennings (mej)
Added sprintf() routines for string classes. Began work on UTF-8/Unicode strings. ---------------------------------------------------------------------- SVN revision: 22350
-rw-r--r--ChangeLog6
-rw-r--r--include/libast/mbuff.h3
-rw-r--r--include/libast/str.h3
-rw-r--r--include/libast/ustr.h16
-rw-r--r--src/mbuff.c33
-rw-r--r--src/str.c33
-rw-r--r--src/ustr.c33
-rw-r--r--test/test.c55
8 files changed, 180 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 5b9a8a4..193cfe0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -781,3 +781,9 @@ Fleshed out ustr class; doesn't do UTF-8 yet though.
Fixed array removal code. I have no idea how that was working before.
----------------------------------------------------------------------
+Tue Apr 25 10:19:10 2006 Michael Jennings (mej)
+
+Added sprintf() routines for string classes.
+
+Began work on UTF-8/Unicode strings.
+----------------------------------------------------------------------
diff --git a/include/libast/mbuff.h b/include/libast/mbuff.h
index 653b54d..ccd0186 100644
--- a/include/libast/mbuff.h
+++ b/include/libast/mbuff.h
@@ -68,6 +68,7 @@
#define SPIF_MBUFF_RINDEX(o, x) SPIF_CAST(memidx) (SPIF_OBJ_CALL_METHOD((o), rindex)(o, x))
#define SPIF_MBUFF_SPLICE(o, n1, n2, x) SPIF_CAST(bool) (SPIF_OBJ_CALL_METHOD((o), splice)(o, n1, n2, x))
#define SPIF_MBUFF_SPLICE_FROM_PTR(o, n1, n2, x) SPIF_CAST(bool) (SPIF_OBJ_CALL_METHOD((o), splice_from_ptr)(o, n1, n2, x))
+#define SPIF_MBUFF_SPRINTF(x) SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), sprintf) x)
#define SPIF_MBUFF_SUBBUFF(o, n1, n2) SPIF_CAST(mbuff) (SPIF_OBJ_CALL_METHOD((o), subbuff)(o, n1, n2))
#define SPIF_MBUFF_SUBBUFF_TO_PTR(o, n1, n2) SPIF_CAST(byteptr) (SPIF_OBJ_CALL_METHOD((o), subbuff_to_ptr)(o, n1, n2))
#define SPIF_MBUFF_TRIM(o) SPIF_CAST(bool) (SPIF_OBJ_CALL_METHOD((o), trim)(o))
@@ -111,6 +112,7 @@ SPIF_DECL_OBJ(mbuffclass) {
spif_func_t rindex;
spif_func_t splice;
spif_func_t splice_from_ptr;
+ spif_func_t sprintf;
spif_func_t subbuff;
spif_func_t subbuff_to_ptr;
spif_func_t trim;
@@ -151,6 +153,7 @@ extern spif_bool_t spif_mbuff_reverse(spif_mbuff_t);
extern spif_memidx_t spif_mbuff_rindex(spif_mbuff_t, spif_uint8_t);
extern spif_bool_t spif_mbuff_splice(spif_mbuff_t, spif_memidx_t, spif_memidx_t, spif_mbuff_t);
extern spif_bool_t spif_mbuff_splice_from_ptr(spif_mbuff_t, spif_memidx_t, spif_memidx_t, spif_byteptr_t, spif_memidx_t);
+extern spif_bool_t spif_mbuff_sprintf(spif_mbuff_t, spif_charptr_t, ...);
extern spif_mbuff_t spif_mbuff_subbuff(spif_mbuff_t, spif_memidx_t, spif_memidx_t);
extern spif_byteptr_t spif_mbuff_subbuff_to_ptr(spif_mbuff_t, spif_memidx_t, spif_memidx_t);
extern spif_bool_t spif_mbuff_trim(spif_mbuff_t);
diff --git a/include/libast/str.h b/include/libast/str.h
index 492e6f5..a20cece 100644
--- a/include/libast/str.h
+++ b/include/libast/str.h
@@ -73,6 +73,7 @@
#define SPIF_STR_RINDEX(o, x) SPIF_CAST(stridx) (SPIF_STR_CALL_METHOD((o), rindex)(o, x))
#define SPIF_STR_SPLICE(o, n1, n2, x) SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), splice)(o, n1, n2, x))
#define SPIF_STR_SPLICE_FROM_PTR(o, n1, n2, x) SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), splice_from_ptr)(o, n1, n2, x))
+#define SPIF_STR_SPRINTF(x) SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), sprintf) x)
#define SPIF_STR_SUBSTR(o, n1, n2) SPIF_CAST(str) (SPIF_STR_CALL_METHOD((o), substr)(o, n1, n2))
#define SPIF_STR_SUBSTR_TO_PTR(o, n1, n2) SPIF_CAST(charptr) (SPIF_STR_CALL_METHOD((o), substr_to_ptr)(o, n1, n2))
#define SPIF_STR_TO_FLOAT(o) SPIF_CAST_C(double) (SPIF_STR_CALL_METHOD((o), to_float)(o))
@@ -128,6 +129,7 @@ SPIF_DECL_OBJ(strclass) {
spif_func_t rindex;
spif_func_t splice;
spif_func_t splice_from_ptr;
+ spif_func_t sprintf;
spif_func_t substr;
spif_func_t substr_to_ptr;
spif_func_t to_float;
@@ -180,6 +182,7 @@ extern spif_bool_t spif_str_reverse(spif_str_t);
extern spif_stridx_t spif_str_rindex(spif_str_t, spif_char_t);
extern spif_bool_t spif_str_splice(spif_str_t, spif_stridx_t, spif_stridx_t, spif_str_t);
extern spif_bool_t spif_str_splice_from_ptr(spif_str_t, spif_stridx_t, spif_stridx_t, spif_charptr_t);
+extern spif_bool_t spif_str_sprintf(spif_str_t, spif_charptr_t, ...);
extern spif_str_t spif_str_substr(spif_str_t, spif_stridx_t, spif_stridx_t);
extern spif_charptr_t spif_str_substr_to_ptr(spif_str_t, spif_stridx_t, spif_stridx_t);
extern double spif_str_to_float(spif_str_t);
diff --git a/include/libast/ustr.h b/include/libast/ustr.h
index 8998bf5..a974439 100644
--- a/include/libast/ustr.h
+++ b/include/libast/ustr.h
@@ -24,6 +24,20 @@
#ifndef _LIBAST_USTR_H_
#define _LIBAST_USTR_H_
+#define SPIF_UTF8_CHAR_LEN(s) ((((SPIF_CAST(uchar) (*(s))) & 0x80) == 0x00) \
+ ? (1) \
+ : ((((SPIF_CAST(uchar) (*(s))) & 0xc0) == 0x80) \
+ ? (2) \
+ : ((((SPIF_CAST(uchar) (*(s))) & 0xe0) == 0xc0) \
+ ? (3) \
+ : ((((SPIF_CAST(uchar) (*(s))) & 0xf0) == 0xe0) \
+ ? (4) \
+ : ((((SPIF_CAST(uchar) (*(s))) & 0xf8) == 0xf0) \
+ ? (5) \
+ : ((((SPIF_CAST(uchar) (*(s))) & 0xfc) == 0xf8) \
+ ? (6) \
+ : (0)))))))
+
#define SPIF_USTR(obj) ((spif_ustr_t) (obj))
#define SPIF_OBJ_IS_USTR(obj) (SPIF_OBJ_IS_TYPE(obj, ustr))
#define SPIF_USTR_ISNULL(s) SPIF_OBJ_ISNULL(SPIF_OBJ(s))
@@ -70,6 +84,7 @@
#define SPIF_USTR_RINDEX(o, x) SPIF_CAST(stridx) (SPIF_OBJ_CALL_METHOD((o), rindex)(o, x))
#define SPIF_USTR_SPLICE(o, n1, n2, x) SPIF_CAST(bool) (SPIF_OBJ_CALL_METHOD((o), splice)(o, n1, n2, x))
#define SPIF_USTR_SPLICE_FROM_PTR(o, n1, n2, x) SPIF_CAST(bool) (SPIF_OBJ_CALL_METHOD((o), splice_from_ptr)(o, n1, n2, x))
+#define SPIF_USTR_SPRINTF(x) SPIF_CAST(bool) (SPIF_STR_CALL_METHOD((o), sprintf) x)
#define SPIF_USTR_SUBSTR(o, n1, n2) SPIF_CAST(ustr) (SPIF_OBJ_CALL_METHOD((o), substr)(o, n1, n2))
#define SPIF_USTR_SUBSTR_TO_PTR(o, n1, n2) SPIF_CAST(charptr) (SPIF_OBJ_CALL_METHOD((o), substr_to_ptr)(o, n1, n2))
#define SPIF_USTR_TO_FLOAT(o) SPIF_CAST_C(double) (SPIF_OBJ_CALL_METHOD((o), to_float)(o))
@@ -134,6 +149,7 @@ extern spif_bool_t spif_ustr_reverse(spif_ustr_t);
extern spif_ustridx_t spif_ustr_rindex(spif_ustr_t, spif_char_t);
extern spif_bool_t spif_ustr_splice(spif_ustr_t, spif_ustridx_t, spif_ustridx_t, spif_ustr_t);
extern spif_bool_t spif_ustr_splice_from_ptr(spif_ustr_t, spif_ustridx_t, spif_ustridx_t, spif_charptr_t);
+extern spif_bool_t spif_ustr_sprintf(spif_ustr_t, spif_charptr_t, ...);
extern spif_ustr_t spif_ustr_substr(spif_ustr_t, spif_ustridx_t, spif_ustridx_t);
extern spif_charptr_t spif_ustr_substr_to_ptr(spif_ustr_t, spif_ustridx_t, spif_ustridx_t);
extern double spif_ustr_to_float(spif_ustr_t);
diff --git a/src/mbuff.c b/src/mbuff.c
index da7d415..2c8e0e4 100644
--- a/src/mbuff.c
+++ b/src/mbuff.c
@@ -66,6 +66,7 @@ static SPIF_CONST_TYPE(mbuffclass) mb_class = {
(spif_func_t) spif_mbuff_rindex,
(spif_func_t) spif_mbuff_splice,
(spif_func_t) spif_mbuff_splice_from_ptr,
+ (spif_func_t) spif_mbuff_sprintf,
(spif_func_t) spif_mbuff_subbuff,
(spif_func_t) spif_mbuff_subbuff_to_ptr,
(spif_func_t) spif_mbuff_trim
@@ -654,6 +655,38 @@ spif_mbuff_splice_from_ptr(spif_mbuff_t self, spif_memidx_t idx, spif_memidx_t c
return TRUE;
}
+spif_bool_t
+spif_mbuff_sprintf(spif_mbuff_t self, spif_charptr_t format, ...)
+{
+ va_list ap;
+
+ ASSERT_RVAL(!SPIF_MBUFF_ISNULL(self), FALSE);
+ va_start(ap, format);
+ if (self->buff != SPIF_NULL_TYPE(byteptr)) {
+ spif_mbuff_done(self);
+ }
+ if (*format == 0) {
+ return TRUE;
+ } else if (*(format + 1) == 0) {
+ return spif_mbuff_init_from_ptr(self, format, 2);
+ } else {
+ int c;
+ char buff[2];
+
+ c = vsnprintf(buff, sizeof(buff), format, ap);
+ if (c <= 0) {
+ return TRUE;
+ } else {
+ c++;
+ self->len = self->size = c;
+ self->buff = SPIF_CAST(charptr) MALLOC(self->size);
+ c = vsnprintf(self->buff, self->size, format, ap);
+ }
+ return ((c >= 0) ? (TRUE) : (FALSE));
+ }
+ ASSERT_NOTREACHED_RVAL(FALSE);
+}
+
spif_mbuff_t
spif_mbuff_subbuff(spif_mbuff_t self, spif_memidx_t idx, spif_memidx_t cnt)
{
diff --git a/src/str.c b/src/str.c
index ebae70e..676e53b 100644
--- a/src/str.c
+++ b/src/str.c
@@ -75,6 +75,7 @@ static SPIF_CONST_TYPE(strclass) s_class = {
(spif_func_t) spif_str_rindex,
(spif_func_t) spif_str_splice,
(spif_func_t) spif_str_splice_from_ptr,
+ (spif_func_t) spif_str_sprintf,
(spif_func_t) spif_str_substr,
(spif_func_t) spif_str_substr_to_ptr,
(spif_func_t) spif_str_to_float,
@@ -706,6 +707,38 @@ spif_str_splice_from_ptr(spif_str_t self, spif_stridx_t idx, spif_stridx_t cnt,
return TRUE;
}
+spif_bool_t
+spif_str_sprintf(spif_str_t self, spif_charptr_t format, ...)
+{
+ va_list ap;
+
+ ASSERT_RVAL(!SPIF_STR_ISNULL(self), FALSE);
+ va_start(ap, format);
+ if (self->s != SPIF_NULL_TYPE(charptr)) {
+ spif_str_done(self);
+ }
+ if (*format == 0) {
+ return TRUE;
+ } else if (*(format + 1) == 0) {
+ return spif_str_init_from_ptr(self, format);
+ } else {
+ int c;
+ char buff[2];
+
+ c = vsnprintf(buff, sizeof(buff), format, ap);
+ if (c <= 0) {
+ return TRUE;
+ } else {
+ self->len = c;
+ self->size = c + 1;
+ self->s = SPIF_CAST(charptr) MALLOC(self->size);
+ c = vsnprintf(self->s, c + 1, format, ap);
+ }
+ return ((c >= 0) ? (TRUE) : (FALSE));
+ }
+ ASSERT_NOTREACHED_RVAL(FALSE);
+}
+
spif_str_t
spif_str_substr(spif_str_t self, spif_stridx_t idx, spif_stridx_t cnt)
{
diff --git a/src/ustr.c b/src/ustr.c
index 75239c6..3361976 100644
--- a/src/ustr.c
+++ b/src/ustr.c
@@ -75,6 +75,7 @@ static SPIF_CONST_TYPE(strclass) s_class = {
(spif_func_t) spif_ustr_rindex,
(spif_func_t) spif_ustr_splice,
(spif_func_t) spif_ustr_splice_from_ptr,
+ (spif_func_t) spif_ustr_sprintf,
(spif_func_t) spif_ustr_substr,
(spif_func_t) spif_ustr_substr_to_ptr,
(spif_func_t) spif_ustr_to_float,
@@ -706,6 +707,38 @@ spif_ustr_splice_from_ptr(spif_ustr_t self, spif_ustridx_t idx, spif_ustridx_t c
return TRUE;
}
+spif_bool_t
+spif_ustr_sprintf(spif_ustr_t self, spif_charptr_t format, ...)
+{
+ va_list ap;
+
+ ASSERT_RVAL(!SPIF_USTR_ISNULL(self), FALSE);
+ va_start(ap, format);
+ if (self->s != SPIF_NULL_TYPE(charptr)) {
+ spif_ustr_done(self);
+ }
+ if (*format == 0) {
+ return TRUE;
+ } else if (*(format + 1) == 0) {
+ return spif_ustr_init_from_ptr(self, format);
+ } else {
+ int c;
+ char buff[2];
+
+ c = vsnprintf(buff, sizeof(buff), format, ap);
+ if (c <= 0) {
+ return TRUE;
+ } else {
+ self->len = c;
+ self->size = c + 1;
+ self->s = SPIF_CAST(charptr) MALLOC(self->size);
+ c = vsnprintf(self->s, c + 1, format, ap);
+ }
+ return ((c >= 0) ? (TRUE) : (FALSE));
+ }
+ ASSERT_NOTREACHED_RVAL(FALSE);
+}
+
spif_ustr_t
spif_ustr_substr(spif_ustr_t self, spif_ustridx_t idx, spif_ustridx_t cnt)
{
diff --git a/test/test.c b/test/test.c
index 4c8815c..2fa1764 100644
--- a/test/test.c
+++ b/test/test.c
@@ -888,6 +888,17 @@ test_str(void)
spif_str_del(teststr);
TEST_PASS();
+ TEST_BEGIN("spif_str_sprintf() function");
+ teststr = spif_str_new();
+ TEST_FAIL_IF(!spif_str_sprintf(teststr, ""));
+ TEST_FAIL_IF(!SPIF_PTR_ISNULL(SPIF_STR_STR(teststr)));
+ TEST_FAIL_IF(!spif_str_sprintf(teststr, "E"));
+ TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, "E"));
+ TEST_FAIL_IF(!spif_str_sprintf(teststr, "float %3.2f int %d string %s", 1.0, 17, "hot"));
+ TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, "float 1.00 int 17 string hot"));
+ spif_str_del(teststr);
+ TEST_PASS();
+
TEST_BEGIN("spif_str_reverse() function");
teststr = spif_str_new_from_buff(buff, sizeof(buff));
spif_str_reverse(teststr);
@@ -1193,6 +1204,17 @@ test_mbuff(void)
spif_mbuff_del(testmbuff);
TEST_PASS();
+ TEST_BEGIN("spif_mbuff_sprintf() function");
+ testmbuff = spif_mbuff_new();
+ TEST_FAIL_IF(!spif_mbuff_sprintf(testmbuff, ""));
+ TEST_FAIL_IF(!SPIF_PTR_ISNULL(SPIF_MBUFF_BUFF(testmbuff)));
+ TEST_FAIL_IF(!spif_mbuff_sprintf(testmbuff, "E"));
+ TEST_FAIL_IF(spif_mbuff_cmp_with_ptr(testmbuff, "E", 2));
+ TEST_FAIL_IF(!spif_mbuff_sprintf(testmbuff, "float %3.2f int %d string %s", 1.0, 17, "hot"));
+ TEST_FAIL_IF(spif_mbuff_cmp_with_ptr(testmbuff, "float 1.00 int 17 string hot", 29));
+ spif_mbuff_del(testmbuff);
+ TEST_PASS();
+
TEST_BEGIN("spif_mbuff_reverse() function");
testmbuff = spif_mbuff_new_from_buff(buff, 6, 0);
spif_mbuff_reverse(testmbuff);
@@ -1209,13 +1231,14 @@ test_mbuff(void)
int
test_ustr(void)
{
- spif_ustr_t testustr, test2ustr;
+ spif_ustr_t testustr, test2ustr, test3ustr;
spif_strclass_t cls;
char tmp[] = "this is a test";
spif_char_t buff[4096] = "abcde";
char tmp2[] = "string #1\nstring #2";
+ char tmp3[] = "ASCII\316\272\341\275\271\317\203\316\274\316ASCII";
FILE *fp;
- int fd, mypipe[2];
+ int i, fd, mypipe[2];
spif_charptr_t foo;
TEST_BEGIN("spif_ustr_new() function");
@@ -1459,6 +1482,17 @@ test_ustr(void)
spif_ustr_del(testustr);
TEST_PASS();
+ TEST_BEGIN("spif_ustr_sprintf() function");
+ testustr = spif_ustr_new();
+ TEST_FAIL_IF(!spif_ustr_sprintf(testustr, ""));
+ TEST_FAIL_IF(!SPIF_PTR_ISNULL(SPIF_USTR_STR(testustr)));
+ TEST_FAIL_IF(!spif_ustr_sprintf(testustr, "E"));
+ TEST_FAIL_IF(spif_ustr_cmp_with_ptr(testustr, "E"));
+ TEST_FAIL_IF(!spif_ustr_sprintf(testustr, "float %3.2f int %d string %s", 1.0, 17, "hot"));
+ TEST_FAIL_IF(spif_ustr_cmp_with_ptr(testustr, "float 1.00 int 17 string hot"));
+ spif_ustr_del(testustr);
+ TEST_PASS();
+
TEST_BEGIN("spif_ustr_reverse() function");
testustr = spif_ustr_new_from_buff(buff, sizeof(buff));
spif_ustr_reverse(testustr);
@@ -1468,6 +1502,23 @@ test_ustr(void)
spif_ustr_del(testustr);
TEST_PASS();
+ TEST_BEGIN("SPIF_UTF8_CHAR_LEN() macro");
+ for (i = 0; i < 5; i++) {
+ TEST_FAIL_IF(SPIF_UTF8_CHAR_LEN(&tmp3[i]) != 1);
+ }
+ TEST_FAIL_IF(SPIF_UTF8_CHAR_LEN(&tmp3[i]) != 3);
+ i+=3;
+ TEST_FAIL_IF(SPIF_UTF8_CHAR_LEN(&tmp3[i]) != 2);
+ i+=2;
+ TEST_FAIL_IF(SPIF_UTF8_CHAR_LEN(&tmp3[i]) != 3);
+ i+=3;
+ TEST_FAIL_IF(SPIF_UTF8_CHAR_LEN(&tmp3[i]) != 2);
+ i+=2;
+ for (; i < 21; i++) {
+ TEST_FAIL_IF(SPIF_UTF8_CHAR_LEN(&tmp3[i]) != 1);
+ }
+ TEST_PASS();
+
TEST_PASSED("spif_ustr_t");
return 0;
}