Skip to content

Commit bf637fa

Browse files
committed
feat(string): Merge startWith/endsWith implementations
1 parent a2c1d0e commit bf637fa

File tree

4 files changed

+101
-36
lines changed

4 files changed

+101
-36
lines changed

Core/GameEngine/Include/Common/UnicodeString.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,30 @@ class UnicodeString
306306
*/
307307
int compareNoCase(const WideChar* s) const;
308308

309+
/**
310+
return true iff self starts with the given string.
311+
*/
312+
Bool startsWith(const WideChar* p) const;
313+
inline Bool startsWith(const UnicodeString& stringSrc) const { return startsWith(stringSrc.str()); }
314+
315+
/**
316+
return true iff self starts with the given string. (case insensitive)
317+
*/
318+
Bool startsWithNoCase(const WideChar* p) const;
319+
inline Bool startsWithNoCase(const UnicodeString& stringSrc) const { return startsWithNoCase(stringSrc.str()); }
320+
321+
/**
322+
return true iff self ends with the given string.
323+
*/
324+
Bool endsWith(const WideChar* p) const;
325+
Bool endsWith(const UnicodeString& stringSrc) const { return endsWith(stringSrc.str()); }
326+
327+
/**
328+
return true iff self ends with the given string. (case insensitive)
329+
*/
330+
Bool endsWithNoCase(const WideChar* p) const;
331+
Bool endsWithNoCase(const UnicodeString& stringSrc) const { return endsWithNoCase(stringSrc.str()); }
332+
309333
/**
310334
conceptually similar to strtok():
311335

Core/GameEngine/Source/Common/System/AsciiString.cpp

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -477,57 +477,25 @@ void AsciiString::format_va(const char* format, va_list args)
477477
// -----------------------------------------------------
478478
Bool AsciiString::startsWith(const char* p) const
479479
{
480-
if (*p == 0)
481-
return true; // everything starts with the empty string
482-
483-
int lenThis = getLength();
484-
int lenThat = strlen(p);
485-
if (lenThis < lenThat)
486-
return false; // that must be smaller than this
487-
488-
return strncmp(peek(), p, lenThat) == 0;
480+
return ::startsWith(peek(), p);
489481
}
490482

491483
// -----------------------------------------------------
492484
Bool AsciiString::startsWithNoCase(const char* p) const
493485
{
494-
if (*p == 0)
495-
return true; // everything starts with the empty string
496-
497-
int lenThis = getLength();
498-
int lenThat = strlen(p);
499-
if (lenThis < lenThat)
500-
return false; // that must be smaller than this
501-
502-
return strnicmp(peek(), p, lenThat) == 0;
486+
return ::startsWithNoCase(peek(), p);
503487
}
504488

505489
// -----------------------------------------------------
506490
Bool AsciiString::endsWith(const char* p) const
507491
{
508-
if (*p == 0)
509-
return true; // everything ends with the empty string
510-
511-
int lenThis = getLength();
512-
int lenThat = strlen(p);
513-
if (lenThis < lenThat)
514-
return false; // that must be smaller than this
515-
516-
return strncmp(peek() + lenThis - lenThat, p, lenThat) == 0;
492+
return ::endsWith(peek(), p);
517493
}
518494

519495
// -----------------------------------------------------
520496
Bool AsciiString::endsWithNoCase(const char* p) const
521497
{
522-
if (*p == 0)
523-
return true; // everything ends with the empty string
524-
525-
int lenThis = getLength();
526-
int lenThat = strlen(p);
527-
if (lenThis < lenThat)
528-
return false; // that must be smaller than this
529-
530-
return strnicmp(peek() + lenThis - lenThat, p, lenThat) == 0;
498+
return ::endsWithNoCase(peek(), p);
531499
}
532500

533501
//-----------------------------------------------------------------------------

Core/GameEngine/Source/Common/System/UnicodeString.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,30 @@ void UnicodeString::format_va(const WideChar* format, va_list args)
406406
}
407407
}
408408

409+
// -----------------------------------------------------
410+
Bool UnicodeString::startsWith(const WideChar* p) const
411+
{
412+
return ::startsWith(peek(), p);
413+
}
414+
415+
// -----------------------------------------------------
416+
Bool UnicodeString::startsWithNoCase(const WideChar* p) const
417+
{
418+
return ::startsWithNoCase(peek(), p);
419+
}
420+
421+
// -----------------------------------------------------
422+
Bool UnicodeString::endsWith(const WideChar* p) const
423+
{
424+
return ::endsWith(peek(), p);
425+
}
426+
427+
// -----------------------------------------------------
428+
Bool UnicodeString::endsWithNoCase(const WideChar* p) const
429+
{
430+
return ::endsWithNoCase(peek(), p);
431+
}
432+
409433
//-----------------------------------------------------------------------------
410434
Bool UnicodeString::nextToken(UnicodeString* tok, UnicodeString delimiters)
411435
{

Core/Libraries/Source/WWVegas/WWLib/stringex.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ template<size_t Size> size_t strlmove_t(char (&dst)[Size], const char *src);
5555
template<size_t Size> size_t strlmcat_t(char (&dst)[Size], const char *src);
5656
#endif
5757

58+
template<typename T> bool startsWith(const T *str, const T *prefix);
59+
template<typename T> bool startsWithNoCase(const T *str, const T *prefix);
60+
template<typename T> bool endsWith(const T *str, const T *suffix);
61+
template<typename T> bool endsWithNoCase(const T *str, const T *suffix);
62+
5863

5964
// Implementation
6065

@@ -182,3 +187,47 @@ template<size_t Size> size_t strlcat_t(char (&dst)[Size], const char *src) { ret
182187
template<size_t Size> size_t strlmove_t(char (&dst)[Size], const char *src) { return strlmove_t(dst, src, Size); }
183188
template<size_t Size> size_t strlmcat_t(char (&dst)[Size], const char *src) { return strlmcat_t(dst, src, Size); }
184189
#endif
190+
191+
inline int compare_string(const char *a, const char *b, const size_t maxCount, const bool caseSensitive = true)
192+
{
193+
return caseSensitive ? strncmp(a, b, maxCount) : _strnicmp(a, b, maxCount);
194+
}
195+
196+
inline int compare_string(const wchar_t *a, const wchar_t *b, const size_t maxCount, const bool caseSensitive = true)
197+
{
198+
return caseSensitive ? wcsncmp(a, b, maxCount) : _wcsnicmp(a, b, maxCount);
199+
}
200+
201+
template<typename T> bool string_edge_equals(const T *str, const T *smaller, const bool endEdge, const bool case_sensitive = true)
202+
{
203+
if (*smaller == T(0))
204+
return true; // everything starts or ends with the empty string
205+
206+
const size_t strlen = strlen_t(str);
207+
const size_t smallen = strlen_t(smaller);
208+
if (strlen < smallen)
209+
return false; // smaller must be smaller than str
210+
211+
const T* strStart = endEdge ? str + strlen - smallen : str;
212+
213+
return compare_string(strStart, smaller, smallen, case_sensitive) == 0;
214+
}
215+
216+
template<typename T> bool startsWith(const T *str, const T *prefix)
217+
{
218+
return string_edge_equals(str, prefix, false);
219+
};
220+
template<typename T> bool startsWithNoCase(const T *str, const T *prefix)
221+
{
222+
return string_edge_equals(str, prefix, false, false);
223+
};
224+
225+
template<typename T> bool endsWith(const T *str, const T *suffix)
226+
{
227+
return string_edge_equals(str, suffix, true);
228+
};
229+
230+
template<typename T> bool endsWithNoCase(const T *str, const T *suffix)
231+
{
232+
return string_edge_equals(str, suffix, true, false);
233+
};

0 commit comments

Comments
 (0)