Permalink
Browse files

provide a basic xstring utilities

  • Loading branch information...
1 parent cc555ca commit bb6d3eab955d52d28dad49b663103ae2b7cbfddf @c9s committed Mar 20, 2013
Showing with 80 additions and 9 deletions.
  1. +80 −9 ext/php_sqlbuilder.c
View
@@ -64,37 +64,108 @@ typedef struct {
-xstring * xstring_new();
-xstring * xstring_new_from_string(char * str, int len);
+PHPAPI xstring * xstring_alloc();
+PHPAPI xstring * xstring_new(int cap);
+PHPAPI xstring * xstring_new_from_string(char * str, int len);
+PHPAPI void xstring_free(xstring *xstr);
+PHPAPI void xstring_scale(xstring *xstr, int size);
+PHPAPI void xstring_scale_large(xstring *xstr, int size);
-xstring * xstring_new()
+
+/* allocate an empty xstring structure */
+PHPAPI xstring * xstring_alloc()
{
return emalloc( sizeof(xstring) );
}
-xstring * xstring_new_from_string(char * str, int len)
+
+PHPAPI xstring * xstring_new(int cap)
+{
+ xstring * x = xstring_alloc();
+ x->cap = cap;
+ x->str = emalloc(sizeof(char) * cap);
+ x->len = 0;
+ return x;
+}
+
+
+/* create an xstring from exsiting string */
+PHPAPI xstring * xstring_new_from_string(char * str, int len)
{
xstring * xstr;
- xstr = xstring_new();
+ xstr = xstring_alloc();
xstr->str = str;
xstr->len = len;
xstr->cap = len;
return xstr;
}
-void xstring_concat_string(xstring * xstr, char * str, int len)
+
+PHPAPI void xstring_concat_string(xstring * xstr, char * str, int len)
{
- if ( len + xstr->len > xstr->cap ) {
+ if ( xstr->len + len > xstr->cap ) {
// do realloc
- xstr->cap += len + 128;
- xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+ xstring_scale(xstr, len);
}
memcpy(xstr->str + xstr->len, str, len);
xstr->len += len;
memcpy(xstr->str + xstr->len, "\0", 1);
}
+PHPAPI void xstring_realloc(xstring *xstr, int size)
+{
+ xstr->cap += size;
+ xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+}
+
+
+PHPAPI void xstring_scale(xstring *xstr, int size)
+{
+ xstr->cap += size + 256;
+ xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+}
+
+PHPAPI void xstring_scale_large(xstring *xstr, int size)
+{
+ xstr->cap += size + 512;
+ xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+}
+
+
+
+PHPAPI xstring * xstring_quote_string(xstring * xstr, char * quote , int quote_len)
+{
+ if ( xstr->len + quote_len > xstr->cap ) {
+ xstring_realloc(xstr, quote_len);
+ }
+
+ xstring *xnewstr;
+
+ if ( xstr->len + quote_len > xstr->cap ) {
+ // create a new empty string with larger capacity
+ xnewstr = xstring_new( xstr->cap + quote_len );
+ } else {
+ xnewstr = xstring_new( xstr->cap );
+ }
+
+ xnewstr->len = xstr->len + quote_len;
+ memcpy(xnewstr, quote, quote_len);
+ memcpy(xnewstr + quote_len, xstr->str, xstr->len);
+ memcpy(xnewstr + quote_len + xstr->len, quote, quote_len);
+ return xnewstr;
+}
+
+PHPAPI void xstring_free(xstring *xstr)
+{
+ // free up the string
+ efree(xstr->str);
+ // free up the structure itself
+ efree(xstr);
+}
+
+
+
PHPAPI void str_column_double_quote(char * str, int str_len, zval * return_value)
{

0 comments on commit bb6d3ea

Please sign in to comment.