Changeset 20 in Main for trunk/Common
- Timestamp:
- Sep 14, 2024, 6:59:15 PM (2 months ago)
- Location:
- trunk/Common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/cm_string.h
r16 r20 6 6 #include <stdbool.h> 7 7 8 int cm_hex(const char* str, int len); 9 char* cm_html_escape(const char* str); 8 10 char* cm_strcat(const char* a, const char* b); 9 11 char* cm_strcat3(const char* a, const char* b, const char* c); -
trunk/Common/string.c
r16 r20 4 4 #include <stdlib.h> 5 5 #include <stdbool.h> 6 #include <stdio.h> 6 7 #include <ctype.h> 7 8 … … 113 114 return true; 114 115 } 116 117 int cm_hex(const char* str, int len) { 118 int n = 0; 119 int i; 120 for(i = 0; i < len; i++) { 121 char c = str[i]; 122 n *= 16; 123 if('0' <= c && c <= '9') { 124 n += c - '0'; 125 } else if('a' <= c && c <= 'f') { 126 n += c - 'a' + 10; 127 } else if('A' <= c && c <= 'F') { 128 n += c - 'A' + 10; 129 } 130 } 131 return n; 132 } 133 134 char* cm_html_escape(const char* str) { 135 int i; 136 char* result = malloc(1); 137 result[0] = 0; 138 char cbuf[2]; 139 cbuf[1] = 0; 140 for(i = 0; str[i] != 0; i++) { 141 cbuf[0] = str[i]; 142 if(str[i] == '&') { 143 char* tmp = result; 144 result = cm_strcat(tmp, "&"); 145 free(tmp); 146 } else if(str[i] == '<') { 147 char* tmp = result; 148 result = cm_strcat(tmp, "<"); 149 free(tmp); 150 } else if(str[i] == '>') { 151 char* tmp = result; 152 result = cm_strcat(tmp, ">"); 153 free(tmp); 154 } else { 155 char* tmp = result; 156 result = cm_strcat(tmp, cbuf); 157 free(tmp); 158 } 159 } 160 return result; 161 }
Note:
See TracChangeset
for help on using the changeset viewer.