[uim-commit] r978 - in branches/r5rs/sigscheme: . test

kzk at freedesktop.org kzk at freedesktop.org
Tue Jul 19 18:13:54 EST 2005


Author: kzk
Date: 2005-07-19 01:13:51 -0700 (Tue, 19 Jul 2005)
New Revision: 978

Modified:
   branches/r5rs/sigscheme/operations.c
   branches/r5rs/sigscheme/sigscheme.c
   branches/r5rs/sigscheme/sigscheme.h
   branches/r5rs/sigscheme/test/test-num.scm
Log:
* This commit implements "number->string" and "string->number". These
  functions doesn't support radix now.

* sigscheme/sigscheme.h
* sigscheme/operations.c
  - (ScmOp_number_to_string, ScmOp_string_to_number): new func
* sigscheme/sigscheme/c
  - (SigScm_Initialize): init subr "number->string" and "string->number"

* sigscheme/test/test-num
  - add testcases for "number->string" and "string->number"


Modified: branches/r5rs/sigscheme/operations.c
===================================================================
--- branches/r5rs/sigscheme/operations.c	2005-07-18 22:00:07 UTC (rev 977)
+++ branches/r5rs/sigscheme/operations.c	2005-07-19 08:13:51 UTC (rev 978)
@@ -35,6 +35,7 @@
   System Include
 =======================================*/
 #include <string.h>
+#include <stdlib.h>
 
 /*=======================================
   Local Include
@@ -239,8 +240,11 @@
     return SCM_FALSE;
 }
 
+/*=======================================
+  R5RS : 6.2 Numbers
+=======================================*/
 /*==============================================================================
-  R5RS : 6.2 Numbers
+  R5RS : 6.2 Numbers : 6.2.5 Numerical Operations
 ==============================================================================*/
 ScmObj ScmOp_plus2n(ScmObj obj1, ScmObj obj2)
 {
@@ -633,6 +637,50 @@
     return Scm_NewInt(n1 % n2);
 }
 
+/*==============================================================================
+  R5RS : 6.2 Numbers : 6.2.6 Numerical input and output
+==============================================================================*/
+/* TODO : support radix */
+ScmObj ScmOp_number_to_string(ScmObj z)
+{
+    int n = 0;
+    int i = 0;
+    int size = 0;
+    char *str = NULL;
+
+    if (EQ(ScmOp_numberp(z), SCM_FALSE))
+	SigScm_ErrorObj("number->string : number required but got ", z);
+
+    /* get value */
+    n = SCM_INT_VALUE(z);
+
+    /* get size */
+    for (size = 1; (int)(n / 10) != 0; size++)
+	n /= 10;
+
+    /* allocate str */
+    str = (char *)malloc(sizeof(char) * size + 1);
+
+    /* fill str */
+    n = SCM_INT_VALUE(z);
+    str[size] = '\0';
+    for (i = size; 0 < i; i--) {
+	str[i - 1] = '0' + (n % 10);
+	n /= 10;
+    }
+
+    return Scm_NewString(str);
+}
+
+/* TODO : support radix */
+ScmObj ScmOp_string_to_number(ScmObj string)
+{
+    if (!SCM_STRINGP(string))
+	SigScm_ErrorObj("string->number : string required but got ", string);
+
+    return Scm_NewInt((int)atof(SCM_STRING_STR(string)));
+}
+
 /*===================================
   R5RS : 6.3 Other data types
 ===================================*/

Modified: branches/r5rs/sigscheme/sigscheme.c
===================================================================
--- branches/r5rs/sigscheme/sigscheme.c	2005-07-18 22:00:07 UTC (rev 977)
+++ branches/r5rs/sigscheme/sigscheme.c	2005-07-19 08:13:51 UTC (rev 978)
@@ -145,6 +145,8 @@
     Scm_InitSubr2("quotient"             , ScmOp_quotient);
     Scm_InitSubr2("modulo"               , ScmOp_modulo);
     Scm_InitSubr2("remainder"            , ScmOp_remainder);
+    Scm_InitSubr1("number->string"       , ScmOp_number_to_string);
+    Scm_InitSubr1("string->number"       , ScmOp_string_to_number);
     Scm_InitSubr1("not"                  , ScmOp_not);
     Scm_InitSubr1("boolean?"             , ScmOp_booleanp);
     Scm_InitSubr1("pair?"                , ScmOp_pairp);

Modified: branches/r5rs/sigscheme/sigscheme.h
===================================================================
--- branches/r5rs/sigscheme/sigscheme.h	2005-07-18 22:00:07 UTC (rev 977)
+++ branches/r5rs/sigscheme/sigscheme.h	2005-07-19 08:13:51 UTC (rev 978)
@@ -167,6 +167,8 @@
 ScmObj ScmOp_quotient(ScmObj n1, ScmObj n2);
 ScmObj ScmOp_modulo(ScmObj n1, ScmObj n2);
 ScmObj ScmOp_remainder(ScmObj n1, ScmObj n2);
+ScmObj ScmOp_number_to_string(ScmObj z);
+ScmObj ScmOp_string_to_number(ScmObj string);
 ScmObj ScmOp_not(ScmObj obj);
 ScmObj ScmOp_booleanp(ScmObj obj);
 ScmObj ScmOp_pairp(ScmObj obj);

Modified: branches/r5rs/sigscheme/test/test-num.scm
===================================================================
--- branches/r5rs/sigscheme/test/test-num.scm	2005-07-18 22:00:07 UTC (rev 977)
+++ branches/r5rs/sigscheme/test/test-num.scm	2005-07-19 08:13:51 UTC (rev 978)
@@ -23,4 +23,12 @@
 (assert-eq? "remainder test3" 1 (remainder 13 -4))
 (assert-eq? "remainder test4" -1 (remainder -13 -4))
 
+(assert-equal? "number->string test1" "1" (number->string 1))
+(assert-equal? "number->string test2" "10" (number->string 10))
+(assert-equal? "number->string test3" "100" (number->string 100))
+
+(assert-eq? "string->number test1" 1   (string->number "1"))
+(assert-eq? "string->number test2" 10  (string->number "10"))
+(assert-eq? "string->number test2" 100 (string->number "100"))
+
 (total-report)



More information about the uim-commit mailing list