Changeset 20 in Main for trunk/Common/string.c
- Timestamp:
- Sep 14, 2024, 6:59:15 PM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.