Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

261 rinda
4.5KB

  1. /* ---------------------------------------------------------------------------|
  2. *
  3. * Distributed under the CC0 public domain license.
  4. * By Alison G. Watson. Attribution is encouraged, though not required.
  5. * See licenses/cc0.txt for more information.
  6. *
  7. * ---------------------------------------------------------------------------|
  8. *
  9. * Various string functions.
  10. *
  11. * ---------------------------------------------------------------------------|
  12. */
  13. #include "common.h"
  14. #include "m_char.h"
  15. #include <stdio.h>
  16. #define CpyStrLocal(out, st) \
  17. do { \
  18. ACS_BeginPrint(); \
  19. ACS_PrintLocalized(st); \
  20. str s = ACS_EndStrParam(); \
  21. for(i32 i = 0, l = ACS_StrLen(s); i <= l; i++) out[i] = s[i]; \
  22. } while(0)
  23. #define StrHashImpl() \
  24. u32 ret = 0; \
  25. for(; *s; s++) ret = *s + 101 * ret; \
  26. return ret
  27. #define X(n, s) str const n = Spf s;
  28. #include "m_stab.h"
  29. str l_strupper(str in)
  30. {
  31. ACS_BeginPrint();
  32. for(astr c = in; *c; c++) ACS_PrintChar(ToUpper(*c));
  33. return ACS_EndStrParam();
  34. }
  35. u32 l_strhash(astr s)
  36. {
  37. StrHashImpl();
  38. }
  39. u32 lstrhash(cstr s)
  40. {
  41. StrHashImpl();
  42. }
  43. char *lstrcpy_str(char *dest, astr src)
  44. {
  45. for(char *i = dest; (*i = *src); ++i, ++src);
  46. return dest;
  47. }
  48. char *lstrcpy2(char *out, cstr s1, cstr s2)
  49. {
  50. char *p = out;
  51. for(; *s1; s1++) *p++ = *s1;
  52. for(; *s2; s2++) *p++ = *s2;
  53. *p++ = '\0';
  54. return out;
  55. }
  56. char *lstrcpy3(char *out, cstr s1, cstr s2, cstr s3)
  57. {
  58. char *p = out;
  59. for(; *s1; s1++) *p++ = *s1;
  60. for(; *s2; s2++) *p++ = *s2;
  61. for(; *s3; s3++) *p++ = *s3;
  62. *p++ = '\0';
  63. return out;
  64. }
  65. i32 lstrcmp_str(cstr s1, astr s2)
  66. {
  67. i32 res;
  68. while((res = *s1 - *s2++) == 0)
  69. if(*s1++ == '\0') break;
  70. return res;
  71. }
  72. i32 faststrcmp(cstr s1, cstr s2)
  73. {
  74. if(s1 == s2) return 0;
  75. i32 res;
  76. while((res = *s1 - *s2++) == 0)
  77. if(*s1++ == '\0') break;
  78. return res;
  79. }
  80. i32 faststrcasecmp(cstr s1, cstr s2)
  81. {
  82. if(s1 == s2) return 0;
  83. i32 res;
  84. while((res = ToUpper(*s1) - ToUpper(*s2++)) == 0)
  85. if(*s1++ == '\0') break;
  86. return res;
  87. }
  88. cstr scoresep(i96 num) {
  89. noinit static char out[48];
  90. if(!num) {
  91. out[0] = '0';
  92. out[1] = '\0';
  93. return out;
  94. }
  95. char *outp = out + countof(out) - 1;
  96. i32 cnum = 0;
  97. while(num) {
  98. lldiv_t div = __div(num, 10LL);
  99. *--outp = div.rem + '0';
  100. num = div.quot;
  101. if(++cnum == 3) {
  102. *--outp = ',';
  103. cnum = 0;
  104. }
  105. }
  106. if(!cnum) outp++;
  107. return outp;
  108. }
  109. cstr alientext(i32 num) {
  110. noinit static char out[80];
  111. if(!num) {
  112. strcpy(out, u8"");
  113. return out;
  114. }
  115. char *outp = out + countof(out) - 1;
  116. i32 cnum = 0;
  117. while(num) {
  118. div_t div = __div(num, 10);
  119. *--outp = 0x80 + div.rem;
  120. *--outp = 0x80;
  121. *--outp = 0xee;
  122. num = div.quot;
  123. if(++cnum == 4) {
  124. *--outp = 0x8a;
  125. *--outp = 0x80;
  126. *--outp = 0xee;
  127. cnum = 0;
  128. }
  129. }
  130. if(!cnum) outp += 3;
  131. return outp;
  132. }
  133. str LanguageV(str name)
  134. {
  135. ACS_BeginPrint();
  136. ACS_PrintLocalized(name);
  137. str ret = ACS_EndStrParam();
  138. while(ret[0] == '$')
  139. {
  140. str sub = ACS_StrMid(ret, 1, INT32_MAX);
  141. ACS_BeginPrint();
  142. ACS_PrintLocalized(sub);
  143. str nex = ACS_EndStrParam();
  144. if(sub != nex) ret = nex;
  145. else break;
  146. }
  147. return ret;
  148. }
  149. char *LanguageVC(char *out, cstr name)
  150. {
  151. noinit static char sbuf[8192];
  152. if(!out) out = sbuf;
  153. CpyStrLocal(out, l_strdup(name));
  154. while(out[0] == '$') CpyStrLocal(out, l_strdup(&out[1]));
  155. return out;
  156. }
  157. char *LanguageCV(char *out, cstr fmt, ...)
  158. {
  159. noinit static char nbuf[256];
  160. va_list vl;
  161. va_start(vl, fmt);
  162. vsprintf(nbuf, fmt, vl);
  163. va_end(vl);
  164. return LanguageVC(out, nbuf);
  165. }
  166. str LanguageNull(cstr fmt, ...)
  167. {
  168. va_list vl;
  169. ACS_BeginPrint();
  170. va_start(vl, fmt);
  171. __vnprintf(fmt, vl);
  172. va_end(vl);
  173. str name = ACS_EndStrParam();
  174. str alias = LanguageV(name);
  175. return name == alias ? snil : alias;
  176. }
  177. noinit static char tcbuf[4096];
  178. #define RemoveTextColorsImpl() \
  179. i32 j = 0; \
  180. \
  181. if(size > countof(tcbuf)) return nil; \
  182. \
  183. for(i32 i = 0; i < size; i++) { \
  184. if(s[i] == '\C') { \
  185. i++; \
  186. if(s[i] == '[') while(s[i] && s[i++] != ']'); \
  187. else i++; \
  188. } \
  189. \
  190. if(i >= size || j >= size || !s[i]) break; \
  191. \
  192. tcbuf[j++] = s[i]; \
  193. } \
  194. \
  195. tcbuf[j++] = '\0'; \
  196. \
  197. return tcbuf;
  198. cstr RemoveTextColors_str(astr s, i32 size) {
  199. RemoveTextColorsImpl();
  200. }
  201. cstr RemoveTextColors(cstr s, i32 size) {
  202. RemoveTextColorsImpl();
  203. }
  204. /* EOF */