rev 596 - in trunk: . src

SVN User <[email protected]> Thu, 10 Jun 2004 15:57:51 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-06-10 15:57:48 -0400 (Thu, 10 Jun 2004)
New Revision: 596

Modified:
   trunk/STATUS.txt
   trunk/src/builtins-string.c
Log:
added (Bytes, encoding) constructor to String

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-06-10 19:29:04 UTC (rev 595)
+++ trunk/STATUS.txt	2004-06-10 19:57:48 UTC (rev 596)
@@ -1,7 +1,6 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
---- allow lists of ints in Bytes constructor
 --- allow Bytes object in String constructor with encoding param
 --- allow lists of ints in Bytes constructor
 

Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c	2004-06-10 19:29:04 UTC (rev 595)
+++ trunk/src/builtins-string.c	2004-06-10 19:57:48 UTC (rev 596)
@@ -118,6 +118,33 @@
 	obj->immutable = TRUE;
 }
 
+//********************************* c2pr_bytcpy *******************************
+void c2pr_bytcpy(isp ist, obj_p obj, u8_t* byt, data_size_t len, int latin) {
+	str_p obj_str;
+	ch3_p ch3p;
+	data_size_t i, size = len*3;
+
+	if (size <= IMM_DATA_LEN) {
+		SET_TYPE_IF_EXC(OBJ(STRING_PROTO), obj, DATA_TYPE_IMMDATA) return;
+		obj->imm_data_len = (int) size;
+		ch3p = obj->data.str;
+	} else {
+		obj_str = obj_malloc(ist, OBJ(STRING_PROTO), obj, sizeof(str_t)+size); if_exc_return;
+		obj_str->size = sizeof(str_t)+size;
+		ch3p = obj_str->str;
+	}
+	for (i=0; i < len; i++, byt++, ch3p++) {
+		if (!latin && *byt > 127) {
+			raise_exception(ist, OBJ(VALUE_EXC), "bad ASCII value (0x%02x), use latin-1 encoding(?)", *byt);
+			return;
+		}
+		ch3p->b0 = *byt;
+		ch3p->b1 = 0;
+		ch3p->b2 = 0;
+	}
+	obj->immutable = TRUE;
+}
+
 //********************************* pr2c_strptr *******************************
 char* pr2c_strptr(isp ist, obj_p str_obj) {
 	str_p strp;
@@ -281,6 +308,24 @@
 				res = long_format(obj->data.ptr, int2i32t(ist, obj2), FALSE);
 				s = pr2c_strptr(ist, res);
 			}
+		} else if (has_proto(ist, obj, OBJ(BYTES_PROTO))) {
+			char* encoding_str;
+			int latin = FALSE;
+			if (llen == 1) 
+				encoding_str = "ASCII";
+			else if (has_proto(ist, obj2 = list_item(ist, parms[1], 1), OBJ(STRING_PROTO)))
+				encoding_str = pr2c_strptr(ist, obj2);
+			else {
+				raise_exception(ist, OBJ(TYPE_EXC), "second param must be an encoding spec (string)");
+				return NULL;
+			}
+			if (!strcmp(encoding_str, "latin-1")) latin = TRUE;
+			else if (strcmp(encoding_str, "ASCII")) {
+				raise_exception(ist, OBJ(TYPE_EXC), "unknown encoding format: \"%s\"", encoding_str);
+				return NULL;
+			}
+			c2pr_bytcpy(ist, self, bytes_ptr(obj), (int) bytes_len(obj), latin); if_exc_return NULL;
+			return OBJ(NONE);
 		} else if (llen == 1) {
 			s = as_str(ist, obj);
 		} else {