1 #include "portability.h"
7 #include "lib/libstring/string.h"
8 #include "../libvector/include/vector.h"
12 #include "elk_config.h"
15 char* jse_strstr(
const char* haystack,
const char* needle) {
16 if (*needle ==
'\0') {
17 return (
char*) haystack;
21 const char* h = haystack;
22 const char* n = needle;
24 while (*h && *n && (*h == *n)) {
30 return (
char*) haystack;
39 void jse_ncpy(
char *destination,
const char *source,
int length) {
40 for (
int i = 0; i < length; i++) {
41 if (source[i] !=
'\0') {
42 destination[i] = source[i];
44 destination[i] =
'\0';
50 void jse_trim(
char *str) {
51 int start = 0, end =
strlen(str) - 1;
54 while (str[start] ==
' ' || str[start] ==
'\t' || str[start] ==
'\n') {
59 while (str[end] ==
' ' || str[end] ==
'\t' || str[end] ==
'\n') {
64 for (
int i = 0; i <= end - start; i++) {
65 str[i] = str[i + start];
69 str[end - start + 1] =
'\0';
73 char* jse_mergeBuffers(
char *buffer1,
char *buffer2,
int bufferSize1,
int bufferSize2) {
76 int mergedSize = bufferSize1 + bufferSize2 ;
77 char *mergeResult = (
char *)calloc(1, (mergedSize + 1) *
sizeof(char));
80 jse_ncpy(mergeResult, buffer1, bufferSize1);
83 jse_ncpy(mergeResult + bufferSize1, buffer2, bufferSize2);
86 mergeResult[mergedSize] =
'\0';
102 void jse_func_tolower(
char* as){
105 if(*as >=
'A' && *as <=
'Z')
116 void jse_func_toupper(
char* as){
119 if(*as >=
'a' && *as <=
'z')
125 char jse_func_char_tolower(
char ch) {
126 if (ch >=
'A' && ch <=
'Z') {
133 int jse_func_atoi(
const char* str) {
139 while (*str ==
' ') {
147 }
else if (*str ==
'+') {
154 if (*str ==
'x' || *str ==
'X') {
164 int digit = *str -
'0';
165 if (digit >= 0 && digit < base) {
166 result = result * base + digit;
167 }
else if (base == 16) {
168 int hex = *str -
'A' + 10;
169 if (hex >= 10 && hex < 16) {
170 result = result * base + hex;
180 return sign * result;
185 int jse_p_int(
const char* str,
char** endptr){
187 bool negative =
false;
192 while (*str ==
' ') {
197 if (*str ==
'-' || *str ==
'+') {
198 sign = (*str++ ==
'-') ? -1 : 1;
202 while (*str >=
'0' && *str <=
'9') {
203 result = result * 10 + (*str++ -
'0');
210 while (*str >=
'0' && *str <=
'9') {
218 if (endptr != NULL) {
219 *endptr = (
char*)str;
225 int jse_getInt(
struct js *
js, jsval_t arg){
226 int type = js_type(arg);
227 if (type == JS_NUM)
return js_getnum(arg);
228 if (type == JS_STR)
return jse_func_atoi(js_str(
js,arg));
229 if (type == JS_UNDEF)
return 0;
230 if (type == JS_NULL)
return 0;
231 if (type == JS_FALSE)
return 0;
232 if (type == JS_TRUE)
return 1;
243 char* jse_strdup(
const char* str) {
244 size_t length =
strlen(str) + 1;
245 char* newStr = calloc(1,length);
247 if (newStr != NULL) {
248 memcpy(newStr, str, length);
size_t strlen(const char *str)
Возращает длину строки
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.