X-Git-Url: https://de.git.xonotic.org/?p=xonotic%2Fxonotic-data.pk3dir.git;a=blobdiff_plain;f=qcsrc%2Flib%2Fstring.qh;h=47a8175cac598554b3f2965a7dfc83db5429d2f5;hp=ca82ecce43f21b69b94c2241338a23df02067159;hb=f72821fdcebe3ca01181a99727a06198de65ea08;hpb=d3e642e032c1e9e62fc5400c14627c54e37e4ae0 diff --git a/qcsrc/lib/string.qh b/qcsrc/lib/string.qh index ca82ecce4..47a8175ca 100644 --- a/qcsrc/lib/string.qh +++ b/qcsrc/lib/string.qh @@ -1,5 +1,8 @@ -#ifndef STRING_H -#define STRING_H +#pragma once + +#include "nil.qh" +#include "sort.qh" +#include "oo.qh" #ifndef SVQC float stringwidth_colors(string s, vector theSize) @@ -13,9 +16,6 @@ } #endif -// Timer (#5) -// -// TODO: macro string seconds_tostring(float sec) { float minutes = floor(sec / 60); @@ -23,6 +23,37 @@ string seconds_tostring(float sec) return sprintf("%d:%02d", minutes, sec); } +string format_time(float seconds) +{ + seconds = floor(seconds + 0.5); + float days = floor(seconds / 864000); + seconds -= days * 864000; + float hours = floor(seconds / 36000); + seconds -= hours * 36000; + float minutes = floor(seconds / 600); + seconds -= minutes * 600; + if (days > 0) return sprintf(_("%d days, %02d:%02d:%02d"), days, hours, minutes, seconds); + else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds); +} + +string mmsss(float tenths) +{ + tenths = floor(tenths + 0.5); + float minutes = floor(tenths / 600); + tenths -= minutes * 600; + string s = ftos(1000 + tenths); + return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1)); +} + +string mmssss(float hundredths) +{ + hundredths = floor(hundredths + 0.5); + float minutes = floor(hundredths / 6000); + hundredths -= minutes * 6000; + string s = ftos(10000 + hundredths); + return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 2)); +} + int ColorTranslateMode; string ColorTranslateRGB(string s) @@ -65,16 +96,16 @@ string CCR(string input) return input; } -bool startsWith(string haystack, string needle) -{ - return substring(haystack, 0, strlen(needle)) == needle; -} +#define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0) bool startsWithNocase(string haystack, string needle) { return strcasecmp(substring(haystack, 0, strlen(needle)), needle) == 0; } +noref string _endsWith_suffix; +#define endsWith(this, suffix) (_endsWith_suffix = suffix, substring(this, -strlen(_endsWith_suffix), -1) == _endsWith_suffix) + /** unzone the string, and return it as tempstring. Safe to be called on string_null */ string fstrunzone(string s) { @@ -84,6 +115,7 @@ string fstrunzone(string s) return sc; } +/** returns first word */ string car(string s) { int o = strstrofs(s, " ", 0); @@ -91,6 +123,7 @@ string car(string s) return substring(s, 0, o); } +/** returns all but first word */ string cdr(string s) { int o = strstrofs(s, " ", 0); @@ -98,6 +131,13 @@ string cdr(string s) return substring(s, o + 1, strlen(s) - (o + 1)); } +string cons(string a, string b) +{ + if (a == "") return b; + if (b == "") return a; + return strcat(a, " ", b); +} + string substring_range(string s, float b, float e) { return substring(s, b, e - b); @@ -171,10 +211,7 @@ string strwords(string s, int w) return substring(s, 0, endpos); } -bool strhasword(string s, string w) -{ - return strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0; -} +#define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0) int u8_strsize(string s) { @@ -188,4 +225,102 @@ int u8_strsize(string s) return l; } -#endif +bool isInvisibleString(string s) +{ + s = strdecolorize(s); + bool utf8 = cvar("utf8_enable"); + for (int i = 0, n = strlen(s); i < n; ++i) + { + int c = str2chr(s, i); + switch (c) + { + case 0: + case 32: // space + break; + case 192: // charmap space + if (!utf8) break; + return false; + case 160: // space in unicode fonts + case 0xE000 + 192: // utf8 charmap space + if (utf8) break; + default: + return false; + } + } + return true; +} + +// Multiline text file buffers + +int buf_load(string pFilename) +{ + int buf = buf_create(); + if (buf < 0) return -1; + int fh = fopen(pFilename, FILE_READ); + if (fh < 0) + { + buf_del(buf); + return -1; + } + string l; + for (int i = 0; (l = fgets(fh)); ++i) + bufstr_set(buf, i, l); + fclose(fh); + return buf; +} + +void buf_save(float buf, string pFilename) +{ + int fh = fopen(pFilename, FILE_WRITE); + if (fh < 0) error(strcat("Can't write buf to ", pFilename)); + int n = buf_getsize(buf); + for (int i = 0; i < n; ++i) + fputs(fh, strcat(bufstr_get(buf, i), "\n")); + fclose(fh); +} + +/** + * converts a number to a string with the indicated number of decimals + * works for up to 10 decimals! + */ +string ftos_decimals(float number, int decimals) +{ + // inhibit stupid negative zero + if (number == 0) number = 0; + // we have sprintf... + return sprintf("%.*f", decimals, number); +} + +int vercmp_recursive(string v1, string v2) +{ + int dot1 = strstrofs(v1, ".", 0); + int dot2 = strstrofs(v2, ".", 0); + string s1 = (dot1 == -1) ? v1 : substring(v1, 0, dot1); + string s2 = (dot2 == -1) ? v2 : substring(v2, 0, dot2); + + float r; + r = stof(s1) - stof(s2); + if (r != 0) return r; + + r = strcasecmp(s1, s2); + if (r != 0) return r; + + if (dot1 == -1) return (dot2 == -1) ? 0 : -1; + else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999)); +} + +int vercmp(string v1, string v2) +{ + if (strcasecmp(v1, v2) == 0) return 0; // early out check + + // "git" beats all + if (v1 == "git") return 1; + if (v2 == "git") return -1; + + return vercmp_recursive(v1, v2); +} + +const string HEXDIGITS = "0123456789ABCDEF0123456789abcdef"; +#define HEXDIGIT_TO_DEC_RAW(d) (strstrofs(HEXDIGITS, (d), 0)) +#define HEXDIGIT_TO_DEC(d) ((HEXDIGIT_TO_DEC_RAW(d) | 0x10) - 0x10) +#define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS, (d), 1))