]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/string.qh
Merge branch 'master' into Lyberta/KillSound
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
index 6f61155800d5af390011bd645b005655b61e14ae..812eb72c8424bf2ab19f3d04fc30bf01522f3213 100644 (file)
@@ -1,11 +1,21 @@
-#ifndef STRING_H
-#define STRING_H
+#pragma once
 
 #include "nil.qh"
 #include "sort.qh"
 #include "oo.qh"
 
-#ifndef SVQC
+#ifdef CSQC
+       float stringwidth_colors(string s, vector theSize)
+       {
+               return stringwidth_builtin(s, true, theSize);
+       }
+
+       float stringwidth_nocolors(string s, vector theSize)
+       {
+               return stringwidth_builtin(s, false, theSize);
+       }
+#endif
+#ifdef MENUQC
        float stringwidth_colors(string s, vector theSize)
        {
                return stringwidth(s, true, theSize);
@@ -17,7 +27,7 @@
        }
 #endif
 
-// TODO: macro
+ERASEABLE
 string seconds_tostring(float sec)
 {
        float minutes = floor(sec / 60);
@@ -25,6 +35,7 @@ string seconds_tostring(float sec)
        return sprintf("%d:%02d", minutes, sec);
 }
 
+ERASEABLE
 string format_time(float seconds)
 {
        seconds = floor(seconds + 0.5);
@@ -38,6 +49,7 @@ string format_time(float seconds)
        else return sprintf(_("%02d:%02d:%02d"), hours, minutes, seconds);
 }
 
+ERASEABLE
 string mmsss(float tenths)
 {
        tenths = floor(tenths + 0.5);
@@ -47,6 +59,7 @@ string mmsss(float tenths)
        return strcat(ftos(minutes), ":", substring(s, 1, 2), ".", substring(s, 3, 1));
 }
 
+ERASEABLE
 string mmssss(float hundredths)
 {
        hundredths = floor(hundredths + 0.5);
@@ -58,11 +71,13 @@ string mmssss(float hundredths)
 
 int ColorTranslateMode;
 
+ERASEABLE
 string ColorTranslateRGB(string s)
 {
        return (ColorTranslateMode & 1) ? strdecolorize(s) : s;
 }
 
+#ifdef GAMEQC
 // color code replace, place inside of sprintf and parse the string... defaults described as constants
 // foreground/normal colors
 string autocvar_hud_colorset_foreground_1 = "2"; // F1 - Green  // primary priority (important names, etc)
@@ -97,15 +112,21 @@ string CCR(string input)
        input = strreplace("^N", "^7", input);  // "none"-- reset to white...
        return input;
 }
+#endif
 
 #define startsWith(haystack, needle) (strstrofs(haystack, needle, 0) == 0)
 
+ERASEABLE
 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 */
+ERASEABLE
 string fstrunzone(string s)
 {
        if (!s) return s;
@@ -115,6 +136,7 @@ string fstrunzone(string s)
 }
 
 /** returns first word */
+ERASEABLE
 string car(string s)
 {
        int o = strstrofs(s, " ", 0);
@@ -123,6 +145,7 @@ string car(string s)
 }
 
 /** returns all but first word */
+ERASEABLE
 string cdr(string s)
 {
        int o = strstrofs(s, " ", 0);
@@ -130,11 +153,29 @@ string cdr(string s)
        return substring(s, o + 1, strlen(s) - (o + 1));
 }
 
+ERASEABLE
+string cons(string a, string b)
+{
+       if (a == "") return b;
+       if (b == "") return a;
+       return strcat(a, " ", b);
+}
+
+ERASEABLE
+string cons_mid(string a, string mid, string b)
+{
+       if (a == "") return b;
+       if (b == "") return a;
+       return strcat(a, mid, b);
+}
+
+ERASEABLE
 string substring_range(string s, float b, float e)
 {
        return substring(s, b, e - b);
 }
 
+ERASEABLE
 string swapwords(string str, float i, float j)
 {
        float n;
@@ -156,10 +197,13 @@ string swapwords(string str, float i, float j)
 }
 
 string _shufflewords_str;
+ERASEABLE
 void _shufflewords_swapfunc(float i, float j, entity pass)
 {
        _shufflewords_str = swapwords(_shufflewords_str, i, j);
 }
+
+ERASEABLE
 string shufflewords(string str)
 {
        _shufflewords_str = str;
@@ -170,6 +214,7 @@ string shufflewords(string str)
        return str;
 }
 
+ERASEABLE
 string unescape(string in)
 {
        in = strzone(in);  // but it doesn't seem to be necessary in my tests at least
@@ -194,6 +239,7 @@ string unescape(string in)
        return str;
 }
 
+ERASEABLE
 string strwords(string s, int w)
 {
        int endpos = 0;
@@ -205,6 +251,7 @@ string strwords(string s, int w)
 
 #define strhasword(s, w) (strstrofs(strcat(" ", s, " "), strcat(" ", w, " "), 0) >= 0)
 
+ERASEABLE
 int u8_strsize(string s)
 {
        int l = 0;
@@ -217,6 +264,8 @@ int u8_strsize(string s)
        return l;
 }
 
+//List of Unicode spaces: https://www.cs.tut.fi/~jkorpela/chars/spaces.html
+ERASEABLE
 bool isInvisibleString(string s)
 {
        s = strdecolorize(s);
@@ -232,8 +281,26 @@ bool isInvisibleString(string s)
                        case 192:          // charmap space
                                if (!utf8) break;
                                return false;
-                       case 160:          // space in unicode fonts
                        case 0xE000 + 192: // utf8 charmap space
+                       case 0x00A0: // NO-BREAK SPACE
+                       //case 0x1680: // OGHAM SPACE MARK
+                       case 0x180E: // MONGOLIAN VOWEL SEPARATOR
+                       case 0x2000: // EN QUAD
+                       case 0x2001: // EM QUAD
+                       case 0x2002: // EN SPACE
+                       case 0x2003: // EM SPACE
+                       case 0x2004: // THREE-PER-EM SPACE
+                       case 0x2005: // FOUR-PER-EM SPACE
+                       case 0x2006: // SIX-PER-EM SPACE
+                       case 0x2007: // FIGURE SPACE
+                       case 0x2008: // PUNCTUATION SPACE
+                       case 0x2009: // THIN SPACE
+                       case 0x200A: // HAIR SPACE
+                       case 0x200B: // ZERO WIDTH SPACE
+                       case 0x202F: // NARROW NO-BREAK SPACE
+                       case 0x205F: // MEDIUM MATHEMATICAL SPACE
+                       case 0x3000: // IDEOGRAPHIC SPACE
+                       case 0xFEFF: // ZERO WIDTH NO-BREAK SPACE
                                if (utf8) break;
                        default:
                                return false;
@@ -244,6 +311,7 @@ bool isInvisibleString(string s)
 
 // Multiline text file buffers
 
+ERASEABLE
 int buf_load(string pFilename)
 {
        int buf = buf_create();
@@ -261,6 +329,7 @@ int buf_load(string pFilename)
        return buf;
 }
 
+ERASEABLE
 void buf_save(float buf, string pFilename)
 {
        int fh = fopen(pFilename, FILE_WRITE);
@@ -273,16 +342,27 @@ void buf_save(float buf, string pFilename)
 
 /**
  * converts a number to a string with the indicated number of decimals
- * works for up to 10 decimals!
  */
+ERASEABLE
 string ftos_decimals(float number, int decimals)
 {
        // inhibit stupid negative zero
        if (number == 0) number = 0;
-       // we have sprintf...
        return sprintf("%.*f", decimals, number);
 }
 
+/**
+ * converts a number to a string with the minimum number of decimals
+ */
+ERASEABLE
+string ftos_mindecimals(float number)
+{
+       // inhibit stupid negative zero
+       if (number == 0) number = 0;
+       return sprintf("%.7g", number);
+}
+
+ERASEABLE
 int vercmp_recursive(string v1, string v2)
 {
        int dot1 = strstrofs(v1, ".", 0);
@@ -301,6 +381,7 @@ int vercmp_recursive(string v1, string v2)
        else return (dot2 == -1) ? 1 : vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
 }
 
+ERASEABLE
 int vercmp(string v1, string v2)
 {
        if (strcasecmp(v1, v2) == 0) return 0;  // early out check
@@ -312,9 +393,12 @@ int vercmp(string v1, string v2)
        return vercmp_recursive(v1, v2);
 }
 
+const string HEXDIGITS_MINSET = "0123456789ABCDEFabcdef";
 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))
+#define DEC_TO_HEXDIGIT(d) (substring(HEXDIGITS_MINSET, (d), 1))
+#define IS_HEXDIGIT(d) (strstrofs(HEXDIGITS_MINSET, (d), 0) >= 0)
 
-#endif
+const string DIGITS = "0123456789";
+#define IS_DIGIT(d) (strstrofs(DIGITS, (d), 0) >= 0)