src/server/ucx/string.c

changeset 16
a9bbd82d2dce
parent 15
cff9c4101dd7
child 21
627b09ee74e4
equal deleted inserted replaced
15:cff9c4101dd7 16:a9bbd82d2dce
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <string.h> 9 #include <string.h>
10 #include <stdarg.h> 10 #include <stdarg.h>
11 11
12 #include "string.h" 12 #include "string.h"
13 #include "mempool.h"
13 14
14 sstr_t sstr (char *s) { 15 sstr_t sstr (char *s) {
15 sstr_t string; 16 sstr_t string;
16 string.ptr = s; 17 string.ptr = s;
17 string.length = strlen(s); 18 string.length = strlen(s);
95 96
96 memcpy(newstring.ptr, s.ptr, s.length); 97 memcpy(newstring.ptr, s.ptr, s.length);
97 98
98 return newstring; 99 return newstring;
99 } 100 }
101
102
103 // webserver extension
104 sstr_t sstrtrim(sstr_t string) {
105 sstr_t newstr = string;
106 int nsoff = 0;
107 int l = 1;
108 for(int i=0;i<string.length;i++) {
109 char c = string.ptr[i];
110 if(l) {
111 /* leading whitespace */
112 if(c > 32) {
113 l = 0;
114 nsoff = i;
115 newstr.ptr = &string.ptr[i];
116 newstr.length = string.length - nsoff;
117 }
118 } else {
119 /* trailing whitespace */
120 if(c > 32) {
121 newstr.length = (i - nsoff) + 1;
122 }
123 }
124 }
125 return newstr;
126 }
127
128 sstr_t sstrdub_mp(UcxMempool *mp, sstr_t s) {
129 sstr_t newstring;
130 newstring.ptr = ucx_mempool_malloc(mp, s.length + 1);
131 newstring.length = s.length;
132 newstring.ptr[newstring.length] = 0;
133
134 memcpy(newstring.ptr, s.ptr, s.length);
135
136 return newstring;
137 }

mercurial