tutil.c 10.7 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
util  
Shengliang Guan 已提交
16 17
#define _DEFAULT_SOURCE
#include "tutil.h"
A
Alex Duan 已提交
18
#include "tlog.h"
H
hzcheng 已提交
19

L
Liu Jicong 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
void *tmemmem(const char *haystack, int32_t hlen, const char *needle, int32_t nlen) {
  const char *limit;

  if (nlen == 0 || hlen < nlen) {
    return NULL;
  }

  limit = haystack + hlen - nlen + 1;
  while ((haystack = (char *)memchr(haystack, needle[0], limit - haystack)) != NULL) {
    if (memcmp(haystack, needle, nlen) == 0) {
      return (void *)haystack;
    }
    haystack++;
  }
  return NULL;
}

H
hzcheng 已提交
37 38 39 40 41 42
int32_t strdequote(char *z) {
  if (z == NULL) {
    return 0;
  }

  int32_t quote = z[0];
43
  if (quote != '\'' && quote != '"' && quote != '`') {
H
hzcheng 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    return (int32_t)strlen(z);
  }

  int32_t i = 1, j = 0;

  while (z[i] != 0) {
    if (z[i] == quote) {
      if (z[i + 1] == quote) {
        z[j++] = (char)quote;
        i++;
      } else {
        z[j++] = 0;
        return (j - 1);
      }
    } else {
      z[j++] = z[i];
    }

    i++;
  }

  return j + 1;  // only one quote, do nothing
}

H
Haojun Liao 已提交
68
size_t strtrim(char *z) {
H
hzcheng 已提交
69 70 71 72
  int32_t i = 0;
  int32_t j = 0;

  int32_t delta = 0;
wmmhello's avatar
wmmhello 已提交
73
  while (isspace(z[j])) {
H
hzcheng 已提交
74 75 76 77 78
    ++j;
  }

  if (z[j] == 0) {
    z[0] = 0;
H
Haojun Liao 已提交
79
    return 0;
H
hzcheng 已提交
80 81 82 83 84 85
  }

  delta = j;

  int32_t stop = 0;
  while (z[j] != 0) {
wmmhello's avatar
wmmhello 已提交
86
    if (isspace(z[j]) && stop == 0) {
H
hzcheng 已提交
87
      stop = j;
wmmhello's avatar
wmmhello 已提交
88
    } else if (!isspace(z[j]) && stop != 0) {
H
hzcheng 已提交
89 90 91 92 93 94 95 96
      stop = 0;
    }

    z[i++] = z[j++];
  }

  if (stop > 0) {
    z[stop - delta] = 0;
H
Haojun Liao 已提交
97
    return (stop - delta);
H
hzcheng 已提交
98 99 100
  } else if (j != i) {
    z[i] = 0;
  }
S
util  
Shengliang Guan 已提交
101

H
Haojun Liao 已提交
102
  return i;
H
hzcheng 已提交
103 104 105 106 107 108
}

char **strsplit(char *z, const char *delim, int32_t *num) {
  *num = 0;
  int32_t size = 4;

wafwerar's avatar
wafwerar 已提交
109
  char **split = taosMemoryMalloc(POINTER_BYTES * size);
H
hzcheng 已提交
110 111 112 113 114 115 116 117 118 119

  for (char *p = strsep(&z, delim); p != NULL; p = strsep(&z, delim)) {
    size_t len = strlen(p);
    if (len == 0) {
      continue;
    }

    split[(*num)++] = p;
    if ((*num) >= size) {
      size = (size << 1);
wafwerar's avatar
wafwerar 已提交
120
      split = taosMemoryRealloc(split, POINTER_BYTES * size);
H
Haojun Liao 已提交
121
      ASSERTS(NULL != split, "realloc memory failed. size=%d", (int32_t) POINTER_BYTES * size);
H
hzcheng 已提交
122 123 124 125 126 127
    }
  }

  return split;
}

128
char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote) {
H
hzcheng 已提交
129
  for (int32_t i = 0; i < len; ++i) {
S
slguan 已提交
130 131 132
    // skip the needle in quote, jump to the end of quoted string
    if (skipquote && (haystack[i] == '\'' || haystack[i] == '"')) {
      char quote = haystack[i++];
S
util  
Shengliang Guan 已提交
133 134
      while (i < len && haystack[i++] != quote)
        ;
S
slguan 已提交
135 136 137 138
      if (i >= len) {
        return NULL;
      }
    }
H
hzcheng 已提交
139

140
    if (haystack[i] == needle) {
141
      return (char *)&haystack[i];
142 143 144 145 146 147
    }
  }

  return NULL;
}

H
Haojun Liao 已提交
148 149 150 151 152 153 154 155 156 157
TdUcs4* wcsnchr(const TdUcs4* haystack, TdUcs4 needle, size_t len) {
  for(int32_t i = 0; i < len; ++i) {
    if (haystack[i] == needle) {
      return (TdUcs4*) &haystack[i];
    }
  }

  return NULL;
}

S
util  
Shengliang Guan 已提交
158
char *strtolower(char *dst, const char *src) {
S
Shengliang Guan 已提交
159
  int32_t esc = 0;
S
util  
Shengliang Guan 已提交
160
  char    quote = 0, *p = dst, c;
weixin_48148422's avatar
weixin_48148422 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174

  for (c = *src++; c; c = *src++) {
    if (esc) {
      esc = 0;
    } else if (quote) {
      if (c == '\\') {
        esc = 1;
      } else if (c == quote) {
        quote = 0;
      }
    } else if (c >= 'A' && c <= 'Z') {
      c -= 'A' - 'a';
    } else if (c == '\'' || c == '"') {
      quote = c;
H
hzcheng 已提交
175
    }
weixin_48148422's avatar
weixin_48148422 已提交
176
    *p++ = c;
H
hzcheng 已提交
177
  }
S
slguan 已提交
178

weixin_48148422's avatar
weixin_48148422 已提交
179 180
  *p = 0;
  return dst;
H
hzcheng 已提交
181
}
S
Shengliang Guan 已提交
182

S
util  
Shengliang Guan 已提交
183
char *strntolower(char *dst, const char *src, int32_t n) {
S
Shengliang Guan 已提交
184
  int32_t esc = 0;
S
util  
Shengliang Guan 已提交
185
  char    quote = 0, *p = dst, c;
186

dengyihao's avatar
dengyihao 已提交
187 188 189
  if (n == 0) {
    *p = 0;
    return dst;
190
  }
191 192 193 194 195 196 197 198 199 200 201
  for (c = *src++; n-- > 0; c = *src++) {
    if (esc) {
      esc = 0;
    } else if (quote) {
      if (c == '\\') {
        esc = 1;
      } else if (c == quote) {
        quote = 0;
      }
    } else if (c >= 'A' && c <= 'Z') {
      c -= 'A' - 'a';
X
Xiaoyu Wang 已提交
202
    } else if (c == '\'' || c == '"' || c == '`') {
203 204 205 206 207 208 209 210
      quote = c;
    }
    *p++ = c;
  }

  *p = 0;
  return dst;
}
H
hzcheng 已提交
211

S
util  
Shengliang Guan 已提交
212
char *strntolower_s(char *dst, const char *src, int32_t n) {
213 214
  char *p = dst, c;
  if (n == 0) {
215
    return NULL;
216 217 218 219 220 221 222 223 224 225 226 227 228 229
  }

  while (n-- > 0) {
    c = *src;
    if (c >= 'A' && c <= 'Z') {
      c -= 'A' - 'a';
    }
    *p++ = c;
    src++;
  }

  return dst;
}

H
hzcheng 已提交
230 231
char *paGetToken(char *string, char **token, int32_t *tokenLen) {
  char quote = 0;
S
util  
Shengliang Guan 已提交
232

H
hzcheng 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  while (*string != 0) {
    if (*string == ' ' || *string == '\t') {
      ++string;
    } else {
      break;
    }
  }

  if (*string == '@') {
    quote = 1;
    string++;
  }

  *token = string;

  while (*string != 0) {
    if (*string == '@' && quote) {
      //*string = 0;
      ++string;
      break;
    }

    if (*string == '#' || *string == '\n' || *string == '\r') {
      *string = 0;
      break;
    }

    if ((*string == ' ' || *string == '\t') && !quote) {
      break;
    } else {
      ++string;
    }
  }

  *tokenLen = (int32_t)(string - *token);
  if (quote) {
    *tokenLen = *tokenLen - 1;
  }

  return string;
}

int64_t strnatoi(char *num, int32_t len) {
  int64_t ret = 0, i, dig, base = 1;

  if (len > (int32_t)strlen(num)) {
    len = (int32_t)strlen(num);
  }

  if ((len > 2) && (num[0] == '0') && ((num[1] == 'x') || (num[1] == 'X'))) {
    for (i = len - 1; i >= 2; --i, base *= 16) {
      if (num[i] >= '0' && num[i] <= '9') {
        dig = (num[i] - '0');
      } else if (num[i] >= 'a' && num[i] <= 'f') {
        dig = num[i] - 'a' + 10;
      } else if (num[i] >= 'A' && num[i] <= 'F') {
        dig = num[i] - 'A' + 10;
      } else {
        return 0;
      }
weixin_48148422's avatar
weixin_48148422 已提交
293
      ret += dig * base;
H
hzcheng 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    }
  } else {
    for (i = len - 1; i >= 0; --i, base *= 10) {
      if (num[i] >= '0' && num[i] <= '9') {
        dig = (num[i] - '0');
      } else {
        return 0;
      }
      ret += dig * base;
    }
  }

  return ret;
}

309 310 311 312
char *strbetween(char *string, char *begin, char *end) {
  char *result = NULL;
  char *_begin = strstr(string, begin);
  if (_begin != NULL) {
S
util  
Shengliang Guan 已提交
313 314
    char   *_end = strstr(_begin + strlen(begin), end);
    int32_t size = (int32_t)(_end - _begin);
315
    if (_end != NULL && size > 0) {
wafwerar's avatar
wafwerar 已提交
316
      result = (char *)taosMemoryCalloc(1, size);
317 318 319 320 321 322
      memcpy(result, _begin + strlen(begin), size - +strlen(begin));
    }
  }
  return result;
}

323 324 325
int32_t tintToHex(uint64_t val, char hex[]) {
  const char hexstr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

326 327 328 329 330 331 332
  int32_t j = 0, k = 0;
  if (val == 0)  {
    hex[j++] = hexstr[0];
    return j;
  }

  // ignore the initial 0
333 334 335 336 337 338 339 340 341 342 343
  while((val & (((uint64_t)0xfL) << ((15 - k) * 4))) == 0) {
    k += 1;
  }

  for (j = 0; k < 16; ++k, ++j) {
    hex[j] = hexstr[(val & (((uint64_t)0xfL) << ((15 - k) * 4))) >> (15 - k) * 4];
  }

  return j;
}

344
int32_t titoa(uint64_t val, size_t radix, char str[]) {
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  if (radix < 2 || radix > 16) {
    return 0;
  }

  const char* s = "0123456789abcdef";
  char buf[65] = {0};

  int32_t i = 0;
  uint64_t v = val;
  while(v > 0) {
    buf[i++] = s[v % radix];
    v /= radix;
  }

  // reverse order
  for(int32_t j = 0; j < i; ++j) {
    str[j] = buf[i - j - 1];
  }

  return i;
}

H
hzcheng 已提交
367 368 369 370 371
int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]) {
  int32_t i;
  char    hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

  for (i = 0; i < len; i++) {
H
hjxilinx 已提交
372
    hexstr[i * 2] = hexval[((bytes[i] >> 4u) & 0xF)];
H
hzcheng 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    hexstr[(i * 2) + 1] = hexval[(bytes[i]) & 0x0F];
  }

  return 0;
}

int32_t taosHexStrToByteArray(char hexstr[], char bytes[]) {
  int32_t len, i;
  char    ch;
  // char *by;

  len = (int32_t)strlen((char *)hexstr) / 2;

  for (i = 0; i < len; i++) {
    ch = hexstr[i * 2];
    if (ch >= '0' && ch <= '9')
      bytes[i] = (char)(ch - '0');
    else if (ch >= 'A' && ch <= 'F')
      bytes[i] = (char)(ch - 'A' + 10);
    else if (ch >= 'a' && ch <= 'f')
      bytes[i] = (char)(ch - 'a' + 10);
    else
      return -1;

    ch = hexstr[i * 2 + 1];
    if (ch >= '0' && ch <= '9')
      bytes[i] = (char)((bytes[i] << 4) + (ch - '0'));
    else if (ch >= 'A' && ch <= 'F')
      bytes[i] = (char)((bytes[i] << 4) + (ch - 'A' + 10));
    else if (ch >= 'a' && ch <= 'f')
      bytes[i] = (char)((bytes[i] << 4) + (ch - 'a' + 10));
    else
      return -1;
  }

  return 0;
}

S
slguan 已提交
411
char *taosIpStr(uint32_t ipInt) {
S
util  
Shengliang Guan 已提交
412
  static char    ipStrArray[3][30];
S
Shengliang Guan 已提交
413
  static int32_t ipStrIndex = 0;
414 415

  char *ipStr = ipStrArray[(ipStrIndex++) % 3];
S
util  
Shengliang Guan 已提交
416 417
  // sprintf(ipStr, "0x%x:%u.%u.%u.%u", ipInt, ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt
  // >> 24));
S
[TD-17]  
slguan 已提交
418
  sprintf(ipStr, "%u.%u.%u.%u", ipInt & 0xFF, (ipInt >> 8) & 0xFF, (ipInt >> 16) & 0xFF, (uint8_t)(ipInt >> 24));
419 420
  return ipStr;
}
S
slguan 已提交
421

S
Shengliang Guan 已提交
422 423 424 425 426 427
void taosIp2String(uint32_t ip, char *str) {
  sprintf(str, "%u.%u.%u.%u", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (uint8_t)(ip >> 24));
}

void taosIpPort2String(uint32_t ip, uint16_t port, char *str) {
  sprintf(str, "%u.%u.%u.%u:%u", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (uint8_t)(ip >> 24), port);
S
Shengliang Guan 已提交
428
}
H
Haojun Liao 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470

size_t tstrncspn(const char *str, size_t size, const char *reject, size_t rsize) {
  if (rsize == 0 || rsize == 1) {
    char* p = strnchr(str, reject[0], size, false);
    return (p == NULL)? size:(p-str);
  }

  /* Use multiple small memsets to enable inlining on most targets.  */
  unsigned char  table[256];
  unsigned char *p = memset(table, 0, 64);
  memset(p + 64, 0, 64);
  memset(p + 128, 0, 64);
  memset(p + 192, 0, 64);

  unsigned char *s = (unsigned char *)reject;
  int32_t index = 0;
  do {
    p[s[index++]] = 1;
  } while (index < rsize);

  s = (unsigned char*) str;
  int32_t times = size >> 2;
  if (times == 0) {
    for(int32_t i = 0; i < size; ++i) {
      if (p[s[i]]) {
        return i;
      }
    }

    return size;
  }

  index = 0;
  uint32_t c0, c1, c2, c3;
  for(int32_t i = 0; i < times; ++i, index += 4) {
    int32_t j = index;
    c0 = p[s[j]];
    c1 = p[s[j + 1]];
    c2 = p[s[j + 2]];
    c3 = p[s[j + 3]];

    if ((c0 | c1 | c2 | c3) != 0) {
D
dapan1121 已提交
471
      size_t count = i * 4;
H
Haojun Liao 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484
      return (c0 | c1) != 0 ? count - c0 + 1 : count - c2 + 3;
    }
  }

  int32_t offset = times * 4;
  for(int32_t i = offset; i < size; ++i) {
    if (p[s[i]]) {
      return i;
    }
  }

  return size;
}
H
Haojun Liao 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498

size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rsize) {
  if (rsize == 0 || rsize == 1) {
    TdUcs4* p = wcsnchr(wcs, reject[0], size);
    return (p == NULL)? size:(p-wcs);
  }

  size_t index = 0;
  while ((index < size) && (wcsnchr(reject, wcs[index], rsize) == NULL)) {
    ++index;
  }

  return index;
}