]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/string.qh
remove multiple declarations of v_angle, document usage of v_angle and angles
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / string.qh
index 6f61155800d5af390011bd645b005655b61e14ae..617891d8b6230f75bafe674b423896d90ac068f1 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,6 @@
        }
 #endif
 
-// TODO: macro
 string seconds_tostring(float sec)
 {
        float minutes = floor(sec / 60);
@@ -105,6 +114,9 @@ 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)
 {
@@ -130,6 +142,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);
@@ -273,16 +292,24 @@ 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!
  */
 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
+ */
+string ftos_mindecimals(float number)
+{
+       // inhibit stupid negative zero
+       if (number == 0) number = 0;
+       return sprintf("%.7g", number);
+}
+
 int vercmp_recursive(string v1, string v2)
 {
        int dot1 = strstrofs(v1, ".", 0);
@@ -312,9 +339,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)