📄 util.vtc
字号:
// Miscellaneous utilitiesfunc nothing() {}func fill_array(ar) { acopy(ar, argv + 1, argc - 1); }func table() [t] { acopy(t = alloc(argc), argv, argc); return t; }func adup(n, a) [t] { t = alloc(n); acopy(t, a, n); return t; }func isdigit(c) --> c >= '0' && c <= '9'func isalpha(c) --> lcase(c) >= 'a' && lcase(c) <= 'z'func isprint(c) --> c > 31 && c != 127func isupper(c) --> c >= 'A' && c <= 'Z'func islower(c) --> c >= 'a' && c <= 'z'func echoln() { callv(.echo, argc, argv, -1, "\n"); }func field(s, l) [r] { strcpy(r = "", s); r[l] = '\0'; return r; }func mod(a, b) --> a >= 0 ? a % b : b - (-a % b)func enum() { while (argc) *argv[--argc] = argc; }func min(a, b) --> a < b ? a : bfunc max(a, b) --> a > b ? a : bfunc skipspaces(s) { while (*s == ' ') s++; return s; }func fcloseall() { while (head(T_FILE)) fclose(head(T_FILE)); }func ptable(a) [t] { acopy(base(t = alloc(argc - 1, a)), argv + 1, argc + 1); return t;}// Contiguous list managementfunc new_list() [list] { list = alloc(argc + 1); *list = argc; acopy(list + 1, argv, argc); return list;}func add_list(list, data / pos) { pos = min(max(pos ? : *list + 1, 1), ++*list); acopy(&list[pos + 1], &list[pos], *list - pos); list[pos] = data;}func del_list(list, pos) { pos = min(max(pos, 1), *list); acopy(&list[pos], &list[pos + 1], (*list)-- + 1 - pos); list[*list + 1] = NULL;}func eq_list_find(list, elem) [i] { for (i = 1; i <= *list && list[i] != elem; i++); return (i <= *list) ? i : NULL;}func list_find_gen(list, e, funcptr) [i] { for (i = 1; i <= *list && (*funcptr)(list[i], e); i++); return (i <= *list) ? i : NULL;}func inteq(a, b) { return !(a == b); }func list_find_eq(list, e) --> list_find_gen(list, e, .inteq)func list_find_strcmp(list, e) --> list_find_gen(list, e, .strcmp)func list_find_stricmp(list, e) --> list_find_gen(list, e, .stricmp)// Translate ^x sequences in a stringfunc ctrl(str) [r, ptr] { r = ""; while (ptr = strchr(str, '^')) { strcat(r, str, ptr - str); if (*++ptr == '^') strcat(r, "^"); else if (*ptr) r[strlen(r)] = mod(ucase(*ptr) - 'A', 128) + 1; str = ptr + 1; } strcat(r, str); return r;}// Produce a readable stringfunc dispstr(str) [r] { for (r = ""; *str; str++) { if (isprint(*str)) *r++ = *str; else { *r++ = '^'; *r++ = (*str + 64) % 128; } } return base(r);}func reverse(s) [l, r, p] { r = "" + strlen(s); while (*s) *--r = *s++; return r;}func rotn(s, n) [r] { for (r = ""; *s; s++) { if (*s >= 'a' && *s <= 'z') *r++ = mod(*s - 'a' + n, 26) + 'a'; else if (*s >= 'A' && *s <= 'Z') *r++ = mod(*s - 'A' + n, 26) + 'A'; } return base(r);}// explodes s around sep, returns list func explode(s, sep) [dest, p, t] { dest = new_list(); while (*s) { p = strstr(s, sep) ? : s + strlen(s); add_list(dest, strcpy("", s, p - s)); s = p + strlen(sep); } return dest;}// implode strings, separating with substrfunc implode(list, substr) [r, i] { if (!*list) return ""; r = strdup(list[1]); for (i = 2; i <= *list; i++) strcat(r, substr + list[i]); return r;}// explodes s around sep, removes adjoining occurences of sepfunc explo(s, sep) [dest, cnt, len, p] { dest = new_list(); while (*s) { p = strstr(s, sep) ? : s + strlen(s); if (p != s) add_list(dest, strcpy("", s, p - s)); s = p + strlen(sep); } return dest;}// Produce a VTC-readable string constantfunc stringconst(s) [r, norm] { r = "\""; while (1) { norm = strcspn(s, "\n\"\\"); strcat(r, s, norm); s += norm; if (!*s) break; strcat(r, "\\"); strcat(r, *s == '"' ? "\"" : *s == '\n' ? "n" : "\\"); s++; } strcat(r, "\""); return r;}// Produce a VTC-readable argument listfunc arglist() [r, i] { for (r = "", i = 0; i < argc; i++) { if (type(argv[i]) == T_INT) strcat(r, itoa(argv[i])); else if (type(argv[i]) == T_SPTR) strcat(r, stringconst(argv[i])); else if (type(argv[i]) == T_FPTR) strcat(r, "." + func_name(argv[i])); if (i < argc - 1) strcat(r, ", "); } return r;}func get_flag(s) { if (!*s) return 2; if (*s == '1' || !stricmp(s, "on")) return 1; if (*s == '0' || !stricmp(s, "off")) return 0; output("Invalid arguments.\n"); return -1;}// Formatted outputfunc cfield(str, len) --> len ? field(str, len) : strfunc bprintf(fmt) [ptr, r, len] { r = ""; ptr = strchr(fmt, '%'); while (ptr) { strcat(r, fmt, ptr - fmt); len = 0; if (*++ptr >= '1' && *ptr <= '9') { len = atoi(ptr); while (*++ptr >= '0' && *ptr <= '9'); } else if (*ptr == '*') { len = *++argv; ptr++; } if (*ptr == '%') strcat(r, "%"); else if (*ptr == 's') strcat(r, cfield(*++argv, len)); else if (*ptr == 'd') strcat(r, cfield(itoa(*++argv), len)); else if (*ptr == 'c') r[strlen(r)] = *++argv; fmt = ptr + 1; ptr = strchr(fmt, '%'); } strcat(r, fmt); return r;}func sprintf(b) { strcpy(b, callv(.bprintf, argc - 1, argv + 1)); }func printf() { if (type(*argv) == T_WIN) output(callv(.bprintf, argc - 1, argv + 1), S_NORM, *argv); else output(callv(.bprintf, argc, argv));}func sendf() { if (type(*argv) == T_RMT) dispatch(callv(.bprintf, argc - 1, argv + 1), *argv); else dispatch(callv(.bprintf, argc, argv));}func fprintf(f) { fwrite(f, callv(.bprintf, argc - 1, argv + 1)); }// Standard argument-parsing functionfunc getopt(s, sw, args) [p, q, on] { while (*s == '-' || *s == '+') { if (!s[1]) return skipspaces(s + 2); on = (*s++ == '+') ? 0 : isupper(*s) ? 2 : 1; while (*s && *s != ' ') { p = strchr(sw, lcase(*s)) ? : strchr(sw, ucase(*s)); if (!p) printf("Unknown switch: %c\n", *s); else if (!isupper(*p)) args[p - sw] = on; else { s = skipspaces(++s); q = strchr(s, ' ') ? : s + strlen(s); strcpy(args[p - sw] = "", s, q - s); s = skipspaces(q); break; } s++; } s = skipspaces(s); } return s;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -