Re[2]: Octstr support for converting to and from HTML 4 entities.

Rene Kluwen / Chimit Software Solutions <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
Hello list,

So I took my own challenge (I was bored ;]).
(Sorry, I will try to stick with only one patch for the same thing, next time I post it.)

Advantages of this patch over the previous one:
- It is faster, because it uses a binary search (didn't really bench
mark it though).
- It is less lines of code (so better to read and less chance on bugs)
- It doesn't rely on c-strings, except for the oct_str functions.
- The oct_str functions are also used for comparing, hencing porting
to other locales does not impose a problem.

In principle, octstr_convert_to_html_entities could be made as fast by
binary searching the table as well. But it requires sorting the table.
If the compiler optimizes well enough (which it does), generating the
entities-table does not require cpu seconds at run-time.

-- Rene...

Monday, September 23, 2002, 1:36:18 AM, you wrote:

RKCSS> Hello Oded,

RKCSS> In your html-entities.def I happened to notice that &amp; was missing!
RKCSS> I don't know if that is on purpose, but if not: now you know ;)

RKCSS> Anyhow... I came up with a faster version. That depends on
RKCSS> html-entities.def being sorted on entity-name.

RKCSS> About being readable: the use of macro's and including an external
RKCSS> file keeps being questionable to me as far as readability is
RKCSS> concerned.
RKCSS> But attached version is more efficient indeed. It uses the macro's in
RKCSS> html-entities.def as well. But I tried to explain as much as possible
RKCSS> in comments.


RKCSS> Sunday, September 22, 2002, 8:09:01 PM, you wrote:

OA>> Hi list.

OA>> We needed the ability to encode data to XML/HTML with HTML 4 entities as defined by W3C, so I thought that adding it to the Octstr infrastructure would be nice - so that other people will be able
OA>> to use it.

OA>> So here it is for your review - don't spare the ammo.

OA>> Both conversions (to HTML entities and from) are using a .def file that is included into the code. the 'to' coversion is very straight forward and should be very easy. I'm afraid that the 'from'
OA>> conversion is rather processor intensive - I'm yet unsure as to how to make it more efficient and still keep it as readable.

OA>> --
OA>> Oded Arbel
OA>> m-Wise mobile solutions
OA>> [email protected]

OA>> +972-9-9581711 (116)
OA>> +972-67-340014

OA>> ::..
OA>> Finagle's laws for existentialists -
OA>> 8.Everything takes longer than you think
 






-- 
Best regards,
 Rene                            mailto:[email protected]
html_entities.patch (application/octet-stream, 3 KB)
Index: gwlib/octstr.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/octstr.c,v
retrieving revision 1.137
diff -u -r1.137 octstr.c
--- gwlib/octstr.c	4 Sep 2002 09:13:11 -0000	1.137
+++ gwlib/octstr.c	23 Sep 2002 00:51:24 -0000
@@ -1220,7 +1220,7 @@
     
     octstr_grow(ostr, ostr->len + 1);
     if (ostr->len > pos)
-        memmove(ostr->data + 1, ostr->data + pos, ostr->len - pos);
+        memmove(ostr->data + pos + 1, ostr->data + pos, ostr->len - pos);
     memcpy(ostr->data + pos, &c, 1);
     ostr->len += 1;
     ostr->data[ostr->len] = '\0';
@@ -2182,3 +2182,78 @@
 
     return resultcode;
 }
+
+/*
+ * This function is meant to find html entities in an octstr.
+ * The html-entities.def file must be sorted alphabetically for
+ * this function to work (according to current Locale in use).
+*/
+static int octstr_find_entity(Octstr* input, int startfind, int endfind)
+{
+#define ENTITY(a,b) { a, b },
+    struct entity_struct {
+	    int entity;
+	    char *entity_str;
+    };
+    const struct entity_struct entities[] = {
+#include "html-entities.def"
+	{ -1, "" } /* pivot */
+    };
+#undef ENTITY
+    int  center;	/* position in table that we are about to compare */
+    int  matchresult;	/* indicates less, equal or greater */
+    char ch,		/* current character in entity-table */
+         current;	/* current character in input string */
+
+    if (endfind == 0) {
+	/* when calling this function we do not (nor even want to) know the
+	 * sizeof(entities). Hence this check. */
+	endfind = (sizeof(entities) / sizeof(struct entity_struct)) -1;
+    }
+    center = startfind + ((endfind - startfind) / 2);
+    matchresult = octstr_str_compare(input, entities[center].entity_str);
+    if (matchresult == 0) {
+	return entities[center].entity;
+    }
+    if (endfind - startfind <= 1) {
+	/* we are at the end of our results */
+	return -1;
+    }
+    if  (matchresult < 0) {
+	/* keep searching in first part of the table */
+	return octstr_find_entity(input, startfind, center);
+    }
+    if (matchresult > 0) {
+	/* keep searching in last part of the table */
+	return octstr_find_entity(input, center, endfind);
+    }
+}
+
+/*
+ * function octstr_convert_from_html_entities()
+ *   convert HTML safe data back to binary data by replacing HTML entities with their 
+ *   respective character values   
+ * Input: data to be inserted in HTML
+ **/
+void octstr_convert_from_html_entities(Octstr* input)
+{
+    int startpos = 0, endpos;
+    int entity;
+    Octstr *match;
+
+    
+    while ((startpos = octstr_search_char(input, '&', startpos)) != -1) {
+	endpos = octstr_search_char(input, ';', startpos + 1);
+	if (endpos >= 0) {
+	    match = octstr_copy(input, startpos + 1, endpos - startpos - 1);
+	    entity = octstr_find_entity(match, 0, 0);
+	    if (entity >= 0) {
+	        octstr_delete(input, startpos, endpos - startpos + 1);
+    	        octstr_insert_char(input, startpos, entity);
+	    }
+	    octstr_destroy(match);
+	}
+	startpos++;
+    }
+}
+
html-entities.def (application/octet-stream, 2 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.