OpenClonk
Standard.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* All kinds of valuable helpers */
19 
20 #include "C4Include.h"
21 #include "lib/Standard.h"
22 
23 //------------------------------------- Basics ----------------------------------------
24 
25 int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
26 {
27  int64_t dx = int64_t(iX1)-iX2, dy = int64_t(iY1)-iY2;
28  int64_t d2 = dx*dx+dy*dy;
29  if (d2 < 0) return -1;
30  int32_t dist = int32_t(sqrt(double(d2)));
31  if (int64_t(dist)*dist < d2) ++dist;
32  if (int64_t(dist)*dist > d2) --dist;
33  return dist;
34 }
35 
36 // Angle between points (iX1, iY1) and (iX2, iY2) with range [0, 360), angle = 0 means vertically upward and increasing angles in clockwise direction.
37 int32_t Angle(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iPrec)
38 {
39  int32_t iAngle;
40  int32_t dx = iX2 - iX1, dy = iY2 - iY1;
41  if (!dx)
42  {
43  if (dy > 0) return 180 * iPrec;
44  else return 0;
45  }
46  if (!dy)
47  {
48  if (dx > 0) return 90 * iPrec;
49  else return 270 * iPrec;
50  }
51 
52  iAngle = static_cast<int32_t>(180.0 * iPrec * atan2(static_cast<double>(Abs(dy)), static_cast<double>(Abs(dx))) / M_PI);
53 
54  if (iX2 > iX1)
55  {
56  if (iY2 < iY1) iAngle = (90 * iPrec) - iAngle;
57  else iAngle = (90 * iPrec) + iAngle;
58  }
59  else
60  {
61  if (iY2 < iY1) iAngle = (270 * iPrec) + iAngle;
62  else iAngle = (270 * iPrec) - iAngle;
63  }
64 
65  return iAngle;
66 }
67 
68 /* Fast integer exponentiation */
69 int Pow(int base, int exponent)
70 {
71  if (exponent < 0) return 0;
72 
73  int result = 1;
74 
75  if (exponent & 1) result = base;
76  exponent >>= 1;
77 
78  while (exponent)
79  {
80  base *= base;
81  if (exponent & 1) result *= base;
82  exponent >>= 1;
83  }
84 
85  return result;
86 }
87 
88 //--------------------------------- Characters ------------------------------------------
89 
90 bool IsIdentifier(char cChar)
91 {
92  if (Inside(cChar,'A','Z')) return true;
93  if (Inside(cChar,'a','z')) return true;
94  if (Inside(cChar,'0','9')) return true;
95  if (cChar=='_') return true;
96  if (cChar=='~') return true;
97  if (cChar=='+') return true;
98  if (cChar=='-') return true;
99  return false;
100 }
101 
102 static bool IsNumber(char c, int base)
103 {
104  return (c >= '0' && c <= '9' && c < ('0' + base)) ||
105  (c >= 'a' && c <= 'z' && c < ('a' + base - 10)) ||
106  (c >= 'A' && c <= 'Z' && c < ('A' + base - 10));
107 }
108 
109 static int ToNumber(char c)
110 {
111  if (c >= '0' && c <= '9') return c - '0';
112  if (c >= 'a' && c <= 'z') return 10 + c - 'a';
113  if (c >= 'A' && c <= 'Z') return 10 + c - 'A';
114  assert(false);
115  return 0;
116 }
117 
118 //------------------------------- Strings ------------------------------------------------
119 
120 int32_t StrToI32(const char *str, int base, const char **scan_end)
121 {
122  const char *s = str;
123  int sign = 1;
124  int32_t result = 0;
125  if (*s == '-')
126  {
127  sign = -1;
128  s++;
129  }
130  else if (*s == '+')
131  {
132  s++;
133  }
134  if (!*s)
135  {
136  // Abort if there are no digits to parse
137  if (scan_end) *scan_end = str;
138  return 0;
139  }
140  while (IsNumber(*s,base))
141  {
142  int value = ToNumber(*s++);
143  assert (value < base && value >= 0);
144  result *= base;
145  result += value;
146  }
147  if (scan_end != nullptr) *scan_end = s;
148  result *= sign;
149  return result;
150 }
151 
152 void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
153 {
154  if (szSource == sTarget) return;
155  if (!sTarget) return;
156  *sTarget=0;
157  if (!szSource) return;
158  while (*szSource && (iMaxL>0))
159  { *sTarget=*szSource; iMaxL--; szSource++; sTarget++; }
160  *sTarget=0;
161 }
162 
163 void SCopy(const char *szSource, char *sTarget)
164 {
165  if (szSource == sTarget) return;
166  if (!sTarget) return;
167  *sTarget=0;
168  if (!szSource) return;
169  strcpy(sTarget,szSource);
170 }
171 
172 void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL, int iIndex)
173 {
174  if (szSource == sTarget) return;
175  if (!sTarget) return;
176  *sTarget=0;
177  if (!szSource) return;
178  while ( *szSource && ((*szSource!=cUntil) || (iIndex>0)) && (iMaxL!=0) )
179  { *sTarget=*szSource; if (*szSource==cUntil) iIndex--; szSource++; sTarget++; iMaxL--; }
180  *sTarget=0;
181 }
182 
183 void SCopyUntil(const char *szSource, char *sTarget, const char * sUntil, size_t iMaxL)
184 {
185  size_t n = std::min(strcspn(szSource, sUntil), iMaxL - 1);
186  strncpy(sTarget, szSource, n);
187  sTarget[n] = 0;
188 }
189 
190 bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild)
191 {
192  if (!szStr1 || !szStr2) return false;
193  while (*szStr1 || *szStr2)
194  {
195  if ((*szStr1==cWild) || (*szStr2==cWild)) return true;
196  if (*szStr1!=*szStr2) return false;
197  szStr1++; szStr2++;
198  }
199  return true;
200 }
201 
202 // Beginning of string 1 needs to match string 2.
203 
204 bool SEqual2(const char *szStr1, const char *szStr2)
205 {
206  if (!szStr1 || !szStr2) return false;
207  while (*szStr1 && *szStr2)
208  if (*szStr1++ != *szStr2++) return false;
209  if (*szStr2) return false; // Str1 is shorter
210  return true;
211 }
212 
213 bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
214 {
215  if (!szStr1 || !szStr2) return false;
216  if (iLen==0) return true;
217  while (*szStr1 && *szStr2)
218  {
219  if ( CharCapital(*szStr1++) != CharCapital(*szStr2++)) return false;
220  if (iLen>0) { iLen--; if (iLen==0) return true; }
221  }
222  if (*szStr1 || *szStr2) return false;
223  return true;
224 }
225 
226 bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
227 {
228  if (!szStr1 || !szStr2) return false;
229  if (iLen==0) return true;
230  while (*szStr1 && *szStr2)
231  {
232  if ( CharCapital(*szStr1++) != CharCapital(*szStr2++)) return false;
233  if (iLen>0) { iLen--; if (iLen==0) return true; }
234  }
235  if (*szStr2) return false; // Str1 is shorter
236  return true;
237 }
238 
239 int SCharPos(char cTarget, const char *szInStr, int iIndex)
240 {
241  const char *cpos;
242  int ccpos;
243  if (!szInStr) return -1;
244  for (cpos=szInStr,ccpos=0; *cpos; cpos++,ccpos++)
245  if (*cpos==cTarget)
246  {
247  if (iIndex==0) return ccpos;
248  else iIndex--;
249  }
250  return -1;
251 }
252 
253 int SCharLastPos(char cTarget, const char *szInStr)
254 {
255  const char *cpos;
256  int ccpos,lcpos;
257  if (!szInStr) return -1;
258  for (cpos=szInStr,ccpos=0,lcpos=-1; *cpos; cpos++,ccpos++)
259  if (*cpos==cTarget) lcpos=ccpos;
260  return lcpos;
261 }
262 
263 void SAppend(const char *szSource, char *szTarget, int iMaxL)
264 {
265  if (iMaxL == -1)
266  SCopy(szSource, szTarget + SLen(szTarget));
267  else
268  SCopy(szSource, szTarget + SLen(szTarget), iMaxL - SLen(szTarget));
269 }
270 
271 void SAppendChar(char cChar, char *szStr)
272 {
273  if (!szStr) return;
274  char *cPos;
275  for (cPos=szStr; *cPos; cPos++) {}
276  *cPos=cChar; *(cPos+1)=0;
277 }
278 
279 bool SCopySegment(const char *szString, int iSegment, char *sTarget,
280  char cSeparator, int iMaxL, bool fSkipWhitespace)
281 {
282  // Advance to indexed segment
283  while (iSegment>0)
284  {
285  if (SCharPos(cSeparator,szString) == -1)
286  { sTarget[0]=0; return false; }
287  szString += SCharPos(cSeparator,szString)+1;
288  iSegment--;
289  }
290  // Advance whitespace
291  if (fSkipWhitespace)
292  szString = SAdvanceSpace(szString);
293  // Copy segment contents
294  SCopyUntil(szString,sTarget,cSeparator,iMaxL);
295  return true;
296 }
297 
298 bool SCopySegmentEx(const char *szString, int iSegment, char *sTarget,
299  char cSep1, char cSep2, int iMaxL, bool fSkipWhitespace)
300 {
301  // Advance to indexed segment
302  while (iSegment>0)
303  {
304  // use the separator that's closer
305  int iPos1 = SCharPos(cSep1,szString), iPos2 = SCharPos(cSep2,szString);
306  if (iPos1 == -1)
307  if (iPos2 == -1)
308  { sTarget[0]=0; return false; }
309  else
310  iPos1=iPos2;
311  else if (iPos2 != -1 && iPos2 < iPos1)
312  iPos1 = iPos2;
313  szString += iPos1+1;
314  iSegment--;
315  }
316  // Advance whitespace
317  if (fSkipWhitespace)
318  szString = SAdvanceSpace(szString);
319  // Copy segment contents; use separator that's closer
320  int iPos1 = SCharPos(cSep1,szString), iPos2 = SCharPos(cSep2,szString);
321  if (iPos2 != -1 && (iPos2 < iPos1 || iPos1 == -1)) cSep1 = cSep2;
322  SCopyUntil(szString,sTarget,cSep1,iMaxL);
323  return true;
324 }
325 
326 unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
327 {
328  unsigned int iResult=0;
329  // Scan string
330  while (*szInStr)
331  {
332  // End position reached (end character is not included)
333  if (szInStr==cpUntil) return iResult;
334  // Character found
335  if (*szInStr==cTarget) iResult++;
336  // Advance
337  szInStr++;
338  }
339  // Done
340  return iResult;
341 }
342 
343 unsigned int SCharCountEx(const char *szString, const char *szCharList)
344 {
345  unsigned int iResult = 0;
346  while ( *szCharList )
347  {
348  iResult += SCharCount( *szCharList, szString );
349  szCharList++;
350  }
351  return iResult;
352 }
353 
354 void SReplaceChar(char *str, char fc, char tc)
355 {
356  while (str && *str)
357  { if (*str==fc) *str=tc; str++; }
358 }
359 
360 void SCapitalize(char *str)
361 {
362  while (str && *str)
363  {
364  *str=CharCapital(*str);
365  str++;
366  }
367 }
368 
369 const char *SSearch(const char *szString, const char *szIndex)
370 {
371  const char *cscr;
372  size_t indexlen,match=0;
373  if (!szString || !szIndex) return nullptr;
374  indexlen=SLen(szIndex);
375  for (cscr=szString; cscr && *cscr; cscr++)
376  {
377  if (*cscr==szIndex[match]) match++;
378  else match=0;
379  if (match>=indexlen) return cscr+1;
380  }
381  return nullptr;
382 }
383 
384 const char *SSearchNoCase(const char *szString, const char *szIndex)
385 {
386  const char *cscr;
387  size_t indexlen,match=0;
388  if (!szString || !szIndex) return nullptr;
389  indexlen=SLen(szIndex);
390  for (cscr=szString; cscr && *cscr; cscr++)
391  {
392  if (CharCapital(*cscr)==CharCapital(szIndex[match])) match++;
393  else match=0;
394  if (match>=indexlen) return cscr+1;
395  }
396  return nullptr;
397 }
398 
399 void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine)
400 {
401  if (!szText) return;
402  // Scan string
403  char *cPos,*cpLastSpace=nullptr;
404  int iLineRun=0;
405  for (cPos=szText; *cPos; cPos++)
406  {
407  // Store last space
408  if (*cPos==cSpace) cpLastSpace=cPos;
409  // Separator encountered: reset line run
410  if (*cPos==cSepa) iLineRun=0;
411  // Need a break, insert at last space
412  if (iLineRun>=iMaxLine)
413  if (cpLastSpace)
414  { *cpLastSpace=cSepa; iLineRun=cPos - cpLastSpace; }
415  // Line run
416  iLineRun++;
417  }
418 }
419 
420 const char *SAdvanceSpace(const char *szSPos)
421 {
422  if (!szSPos) return nullptr;
423  while (IsWhiteSpace(*szSPos)) szSPos++;
424  return szSPos;
425 }
426 
427 const char *SRewindSpace(const char *szSPos, const char *pBegin)
428 {
429  if (!szSPos || !pBegin) return nullptr;
430  while (IsWhiteSpace(*szSPos))
431  {
432  szSPos--;
433  if (szSPos<pBegin) return nullptr;
434  }
435  return szSPos;
436 }
437 
438 const char *SAdvancePast(const char *szSPos, char cPast)
439 {
440  if (!szSPos) return nullptr;
441  while (*szSPos)
442  {
443  if (*szSPos==cPast) { szSPos++; break; }
444  szSPos++;
445  }
446  return szSPos;
447 }
448 
449 void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL)
450 {
451  if (!szSource || !sTarget) return;
452  while (IsIdentifier(*szSource))
453  {
454  if (iMaxL==1) { *sTarget++ = *szSource++; break; }
455  iMaxL--;
456  *sTarget++ = *szSource++;
457  }
458  *sTarget=0;
459 }
460 
461 int SClearFrontBack(char *szString, char cClear)
462 {
463  int cleared=0;
464  char *cpos;
465  if (!szString) return 0;
466  for (cpos=szString; *cpos && (*cpos==cClear); cpos++,cleared++) {}
467  // strcpy is undefined when used on overlapping strings...
468  if (cpos!=szString) memmove(szString, cpos, SLen(cpos) + 1);
469  for (cpos=szString+SLen(szString)-1; (cpos>szString) && (*cpos==cClear); cpos--,cleared++)
470  *cpos=0x00;
471  return cleared;
472 }
473 
474 void SNewSegment(char *szStr, const char *szSepa)
475 {
476  if (szStr[0]) SAppend(szSepa,szStr);
477 }
478 
479 int SGetLine(const char *szText, const char *cpPosition)
480 {
481  if (!szText || !cpPosition) return 0;
482  int iLines = 1;
483  while (*szText && (szText<cpPosition))
484  {
485  if (*szText == 0x0A) iLines++;
486  szText++;
487  }
488  return iLines;
489 }
490 
491 int SLineGetCharacters(const char *szText, const char *cpPosition)
492 {
493  if (!szText || !cpPosition) return 0;
494  int iChars = 1;
495  while (*szText && (szText<cpPosition))
496  {
497  if (*szText == 0x0A)
498  iChars = 1;
499  else if (*szText == '\t')
500  // assume a tab stop every 8 characters
501  iChars = ((iChars - 1 + 8) & ~7) + 1;
502  else
503  iChars++;
504  szText++;
505  }
506  return iChars;
507 }
508 
509 void SInsert(char *szString, const char *szInsert, int iPosition, int iMaxLen)
510 {
511  // Safety
512  if (!szString || !szInsert || !szInsert[0]) return;
513  size_t insertlen = strlen(szInsert);
514  if (iMaxLen >= 0 && strlen(szString) + insertlen > (size_t) iMaxLen) return;
515  // Move up string remainder
516  memmove (szString + iPosition + insertlen, szString + iPosition, SLen(szString+ iPosition) + 1);
517  // Copy insertion
518  MemCopy( szInsert, szString+iPosition, SLen(szInsert) );
519 }
520 
521 void SDelete(char *szString, int iLen, int iPosition)
522 {
523  // Safety
524  if (!szString) return;
525  // Move down string remainder
526  MemCopy( szString+iPosition+iLen, szString+iPosition, SLen(szString+iPosition+iLen)+1 );
527 }
528 
529 bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
530 {
531  int iPos,iLen;
532  if (!szSource || !sTarget) return false;
533  if ((iPos = SCharPos(cOpen,szSource)) < 0) return false;
534  if ((iLen = SCharPos(cClose,szSource+iPos+1)) < 0) return false;
535  SCopy(szSource+iPos+1,sTarget,std::min(iLen,iSize));
536  return true;
537 }
538 
539 bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize)
540 {
541  if (!szList || !sTarget) return false;
542  if (!SCopySegment(szList,iIndex,sTarget,';',iSize)) return false;
543  SClearFrontBack(sTarget);
544  return true;
545 }
546 
547 bool SIsModule(const char *szList, const char *szString, int *ipIndex, bool fCaseSensitive)
548 {
549  char szModule[1024+1];
550  // Compare all modules
551  for (int iMod=0; SGetModule(szList,iMod,szModule,1024); iMod++)
552  if (fCaseSensitive ? SEqual(szString,szModule) : SEqualNoCase(szString,szModule))
553  {
554  // Provide index if desired
555  if (ipIndex) *ipIndex = iMod;
556  // Found
557  return true;
558  }
559  // Not found
560  return false;
561 }
562 
563 bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive)
564 {
565  // Safety / no empties
566  if (!szList || !szModule || !szModule[0]) return false;
567  // Already a module?
568  if (SIsModule(szList,szModule,nullptr,fCaseSensitive)) return false;
569  // New segment, add string
570  SNewSegment(szList);
571  SAppend(szModule,szList);
572  // Success
573  return true;
574 }
575 
576 bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive)
577 {
578  // Safety / no empties
579  if (!szList || !szModules || !szModules[0]) return false;
580  // Add modules
581  char szModule[1024+1]; // limited
582  for (int cnt=0; SGetModule(szModules,cnt,szModule,1024); cnt++)
583  SAddModule(szList,szModule,fCaseSensitive);
584  // Success
585  return true;
586 }
587 
588 bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive)
589 {
590  int iMod,iPos,iLen;
591  // Not a module
592  if (!SIsModule(szList,szModule,&iMod,fCaseSensitive)) return false;
593  // Get module start
594  iPos = 0;
595  if (iMod > 0) iPos = SCharPos(';',szList,iMod-1)+1;
596  // Get module length
597  iLen = SCharPos(';',szList+iPos);
598  if (iLen<0) iLen=SLen(szList); else iLen+=1;
599  // Delete module
600  SDelete(szList,iLen,iPos);
601  // Success
602  return true;
603 }
604 
605 bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive)
606 {
607  // Safety / no empties
608  if (!szList || !szModules || !szModules[0]) return false;
609  // Remove modules
610  char szModule[1024+1]; // limited
611  for (int cnt=0; SGetModule(szModules,cnt,szModule,1024); cnt++)
612  SRemoveModule(szList,szModule,fCaseSensitive);
613  // Success
614  return true;
615 }
616 
617 int SModuleCount(const char *szList)
618 {
619  if (!szList) return 0;
620  int iCount = 0;
621  bool fNewModule = true;
622  while (*szList)
623  {
624  switch (*szList)
625  {
626  case ' ': break;
627  case ';': fNewModule=true; break;
628  default: if (fNewModule) iCount++; fNewModule=false; break;
629  }
630  szList++;
631  }
632  return iCount;
633 }
634 
635 bool SWildcardMatchEx(const char *szString, const char *szWildcard)
636 {
637  // safety
638  if (!szString || !szWildcard) return false;
639  // match char-wise
640  const char *pWild = szWildcard, *pPos = szString;
641  const char *pLWild = nullptr, *pLPos = nullptr; // backtracking
642  while (*pWild || pLWild)
643  // string wildcard?
644  if (*pWild == '*')
645  { pLWild = ++pWild; pLPos = pPos; }
646  // nothing left to match?
647  else if (!*pPos)
648  break;
649  // equal or one-character-wildcard? proceed
650  else if (*pWild == '?' || *pWild == *pPos)
651  { pWild++; pPos++; }
652  // backtrack possible?
653  else if (pLPos)
654  { pWild = pLWild; pPos = ++pLPos; }
655  // match failed
656  else
657  return false;
658  // match complete if both strings are fully matched
659  return !*pWild && !*pPos;
660 }
661 
662 // UTF-8 conformance checking
663 namespace
664 {
665  static const int utf8_continuation_byte_table[256] =
666  {
667  // How many continuation bytes must follow a byte with this value?
668  // Negative values mean that this byte can never start a valid
669  // UTF-8 sequence.
670  // Note that while the encoding scheme allows more than three
671  // trailing bytes in principle, it is not actually allowed for UTF-8.
672  // Values 0xC0 and 0xC1 can never occur in UTF-8 because they
673  // would mark the beginning of an overlong encoding of characters
674  // below 0x80.
675  // Values 0xF5 to 0xFD are invalid because they can only be used
676  // to encode characters beyond the Unicode range.
677  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b00000000..0b00001111, 0x00..0x0F
678  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b00010000..0b00011111, 0x10..0x1F
679  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b00100000..0b00101111, 0x20..0x2F
680  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b00110000..0b00111111, 0x30..0x3F
681  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b01000000..0b01001111, 0x40..0x4F
682  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b01010000..0b01011111, 0x50..0x5F
683  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b01100000..0b01101111, 0x60..0x6F
684  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0b01110000..0b01111111, 0x70..0x7F
685  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0b10000000..0b10001111, 0x80..0x8F
686  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0b10010000..0b10011111, 0x90..0x9F
687  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0b10100000..0b10101111, 0xA0..0xAF
688  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0b10110000..0b10111111, 0xB0..0xBF
689  -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0b11000000..0b11001111, 0xC0..0xCF
690  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0b11010000..0b11011111, 0xD0..0xDF
691  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0b11100000..0b11101111, 0xE0..0xEF
692  3, 3, 3, 3, 3, -3, -3, -3, -4, -4, -4, -4, -5, -5, -1, -1 // 0b11110000..0b11111111, 0xF0..0xFF
693  };
694  static const uint32_t utf8_min_char_value[4] =
695  {
696  // Which is the lowest character value that may be encoded
697  // using this many continuation bytes?
698  0, 0x80, 0x800, 0x10000
699  };
700 }
701 
702 bool IsValidUtf8(const char *text, int length)
703 {
704  // Intentionally using a C-style cast to always get a uint8_t* from char*;
705  // reinterpret_cast would fail here on platforms that have unsigned char,
706  // while static_cast would fail on platforms with a signed char type
707  const uint8_t *input = (const uint8_t*)(text);
708 
709  for (const uint8_t *cursor = input; length < 0 ? *cursor != 0 : cursor - input < length; ++cursor)
710  {
711  int continuation_bytes = utf8_continuation_byte_table[*cursor];
712  if (continuation_bytes < 0)
713  return false;
714  else if (continuation_bytes == 0)
715  {
716  // Standard 7-bit ASCII value (i.e., 1 byte codepoint)
717  continue;
718  }
719  else if (length >= 0 && cursor - input + continuation_bytes >= length)
720  {
721  // Too few remaining bytes
722  return false;
723  }
724 
725  // Compute character value, so we can detect overlong sequences
726  assert((*cursor & 0xC0) == 0xC0);
727  uint32_t value = *cursor;
728  // strip length bits off the start byte
729  value &= (0xFF >> (continuation_bytes + 1));
730  for (int byte = 0; byte < continuation_bytes; ++byte)
731  {
732  // check that this is actually a continuation byte
733  if ((cursor[byte + 1] & 0xC0) != 0x80)
734  return false;
735  // merge continuation byte into value
736  value <<= 6;
737  value |= cursor[byte + 1] & 0x3F;
738  }
739  // make sure this is not overlong
740  if (value < utf8_min_char_value[continuation_bytes])
741  return false;
742  // and also not beyond 0x10FFFF
743  if (value > 0x10FFFF)
744  return false;
745  // and also not a wrongly encoded UTF-16 surrogate half
746  if (value >= 0xD800 && value <= 0xDFFF)
747  return false;
748  cursor += continuation_bytes;
749  }
750  // Looks fine
751  return true;
752 }
753 
754 // UTF-8 iteration
755 uint32_t GetNextUTF8Character(const char **pszString)
756 {
757  // assume the current character is UTF8 already (i.e., highest bit set)
758  const uint32_t REPLACEMENT_CHARACTER = 0xFFFDu;
759  const char *szString = *pszString;
760  unsigned char c = *szString++;
761  uint32_t dwResult = REPLACEMENT_CHARACTER;
762  assert(c>127);
763  if (c>191 && c<224)
764  {
765  unsigned char c2 = *szString++;
766  if ((c2 & 192) != 128) { *pszString = szString; return REPLACEMENT_CHARACTER; }
767  dwResult = (int(c&31)<<6) | (c2&63); // two char code
768  }
769  else if (c >= 224 && c <= 239)
770  {
771  unsigned char c2 = *szString++;
772  if ((c2 & 192) != 128) { *pszString = szString; return REPLACEMENT_CHARACTER; }
773  unsigned char c3 = *szString++;
774  if ((c3 & 192) != 128) { *pszString = szString; return REPLACEMENT_CHARACTER; }
775  dwResult = (int(c&15)<<12) | (int(c2&63)<<6) | int(c3&63); // three char code
776  }
777  else if (c >= 240 && c <= 247)
778  {
779  unsigned char c2 = *szString++;
780  if ((c2 & 192) != 128) { *pszString = szString; return REPLACEMENT_CHARACTER; }
781  unsigned char c3 = *szString++;
782  if ((c3 & 192) != 128) { *pszString = szString; return REPLACEMENT_CHARACTER; }
783  unsigned char c4 = *szString++;
784  if ((c4 & 192) != 128) { *pszString = szString; return REPLACEMENT_CHARACTER; }
785  dwResult = (int(c&7)<<18) | (int(c2&63)<<12) | (int(c3&63)<<6) | int(c4&63); // four char code
786  }
787  *pszString = szString;
788  return dwResult;
789 }
790 
791 int GetCharacterCount(const char * s)
792 {
793  int l = 0;
794  while (*s)
795  {
796  unsigned char c = *s;
797  if (c < 128 || c > 247)
798  {
799  ++l;
800  s += 1;
801  }
802  else if (c > 191 && c < 224)
803  {
804  ++l;
805  s += 2;
806  }
807  else if (c >= 224 && c <= 239)
808  {
809  ++l;
810  s += 3;
811  }
812  else if (c >= 240 && c <= 247)
813  {
814  ++l;
815  s += 4;
816  }
817  else assert(false);
818  }
819  return l;
820 }
821 
822 std::string vstrprintf(const char *format, va_list args)
823 {
824  va_list argcopy;
825  va_copy(argcopy, args);
826  int size = vsnprintf(nullptr, 0, format, argcopy);
827  if (size < 0)
828  throw std::invalid_argument("invalid argument to strprintf");
829  va_end(argcopy);
830  std::string s;
831  s.resize(size + 1);
832  size = vsnprintf(&s[0], s.size(), format, args);
833  assert(size >= 0);
834  s.resize(size);
835  return s;
836 }
837 
838 std::string strprintf(const char *format, ...)
839 {
840  va_list args;
841  va_start(args, format);
842  std::string s = vstrprintf(format, args);
843  va_end(args);
844  return s;
845 }
#define s
int iResult
Definition: C4GroupMain.cpp:40
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:326
bool SWildcardMatchEx(const char *szString, const char *szWildcard)
Definition: Standard.cpp:635
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:354
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:369
bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
Definition: Standard.cpp:529
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:239
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:204
bool SIsModule(const char *szList, const char *szString, int *ipIndex, bool fCaseSensitive)
Definition: Standard.cpp:547
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:279
bool SEqualUntil(const char *szStr1, const char *szStr2, char cWild)
Definition: Standard.cpp:190
bool IsIdentifier(char cChar)
Definition: Standard.cpp:90
int32_t Angle(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iPrec)
Definition: Standard.cpp:37
bool SCopySegmentEx(const char *szString, int iSegment, char *sTarget, char cSep1, char cSep2, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:298
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
int SGetLine(const char *szText, const char *cpPosition)
Definition: Standard.cpp:479
bool SRemoveModule(char *szList, const char *szModule, bool fCaseSensitive)
Definition: Standard.cpp:588
bool SGetModule(const char *szList, int iIndex, char *sTarget, int iSize)
Definition: Standard.cpp:539
void SInsert(char *szString, const char *szInsert, int iPosition, int iMaxLen)
Definition: Standard.cpp:509
int32_t StrToI32(const char *str, int base, const char **scan_end)
Definition: Standard.cpp:120
void SWordWrap(char *szText, char cSpace, char cSepa, int iMaxLine)
Definition: Standard.cpp:399
int Pow(int base, int exponent)
Definition: Standard.cpp:69
const char * SAdvanceSpace(const char *szSPos)
Definition: Standard.cpp:420
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:25
int SLineGetCharacters(const char *szText, const char *cpPosition)
Definition: Standard.cpp:491
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:213
bool IsValidUtf8(const char *text, int length)
Definition: Standard.cpp:702
bool SRemoveModules(char *szList, const char *szModules, bool fCaseSensitive)
Definition: Standard.cpp:605
void SNewSegment(char *szStr, const char *szSepa)
Definition: Standard.cpp:474
int SModuleCount(const char *szList)
Definition: Standard.cpp:617
int GetCharacterCount(const char *s)
Definition: Standard.cpp:791
std::string vstrprintf(const char *format, va_list args)
Definition: Standard.cpp:822
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:271
void SCopyUntil(const char *szSource, char *sTarget, char cUntil, int iMaxL, int iIndex)
Definition: Standard.cpp:172
void SCapitalize(char *str)
Definition: Standard.cpp:360
std::string strprintf(const char *format,...)
Definition: Standard.cpp:838
void SDelete(char *szString, int iLen, int iPosition)
Definition: Standard.cpp:521
bool SAddModules(char *szList, const char *szModules, bool fCaseSensitive)
Definition: Standard.cpp:576
uint32_t GetNextUTF8Character(const char **pszString)
Definition: Standard.cpp:755
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:253
int SClearFrontBack(char *szString, char cClear)
Definition: Standard.cpp:461
const char * SRewindSpace(const char *szSPos, const char *pBegin)
Definition: Standard.cpp:427
bool SAddModule(char *szList, const char *szModule, bool fCaseSensitive)
Definition: Standard.cpp:563
const char * SSearchNoCase(const char *szString, const char *szIndex)
Definition: Standard.cpp:384
unsigned int SCharCountEx(const char *szString, const char *szCharList)
Definition: Standard.cpp:343
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:263
void SCopyIdentifier(const char *szSource, char *sTarget, int iMaxL)
Definition: Standard.cpp:449
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:226
const char * SAdvancePast(const char *szSPos, char cPast)
Definition: Standard.cpp:438
bool IsWhiteSpace(char cChar)
Definition: Standard.h:72
T Abs(T val)
Definition: Standard.h:42
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
void MemCopy(const void *lpMem1, void *lpMem2, size_t dwSize)
Definition: Standard.h:65
char CharCapital(char cChar)
Definition: Standard.h:70
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
size_t SLen(const char *sptr)
Definition: Standard.h:74
int iSize
Definition: TstC4NetIO.cpp:32