Leviathan  0.8.0.0
Leviathan game engine
Leviathan::StringOperations Class Reference

Singleton class that has string processing functions. More...

#include <StringOperations.h>

Public Member Functions

template<>
DLLEXPORT void MakeString (std::wstring &str, const char *characters, size_t count)
 

Static Public Member Functions

template<typename CharType >
static bool IsCharacterWhitespace (CharType character)
 
template<typename CharType >
static bool IsCharacterQuote (CharType character)
 
template<class StringTypeN >
static void MakeString (StringTypeN &str, const char *characters, size_t count)
 
template<class StringTypeN >
static StringTypeN RepeatCharacter (int character, size_t count)
 
template<class StringTypeN >
static const StringTypeN RemoveExtension (const StringTypeN &filepath, bool delpath=true)
 
template<class StringTypeN >
static const StringTypeN GetExtension (const StringTypeN &filepath)
 
template<class StringTypeN >
static const StringTypeN ChangeExtension (const StringTypeN &filepath, const StringTypeN &newext)
 
template<class StringTypeN >
static const StringTypeN RemovePath (const StringTypeN &filepath)
 
template<class StringTypeN >
static const StringTypeN GetPath (const StringTypeN &filepath)
 Returns the path part of a path+filename. More...
 
static DLLEXPORT std::string URLProtocol (const std::string &url)
 
static DLLEXPORT std::string BaseHostName (const std::string &url)
 
static DLLEXPORT std::string URLPath (const std::string &url)
 
static DLLEXPORT std::string CombineURL (const std::string &first, const std::string &second)
 
static DLLEXPORT std::string RemovePartsBeforeAbsoluteURLParts (const std::string &url)
 
static DLLEXPORT bool IsURLDomain (const std::string &str)
 Returns true if string looks like a top level domain. More...
 
template<class StringTypeN >
static const StringTypeN ChangeLineEndsToWindows (const StringTypeN &input)
 Changes all line separators to Windows line separators. More...
 
template<class StringTypeN >
static const StringTypeN ChangeLineEndsToUniversal (const StringTypeN &input)
 Changes all line separators to universal line separators. More...
 
template<class StringTypeN >
static size_t CutLines (const StringTypeN &input, std::vector< StringTypeN > &output)
 Splits a string on line separators. More...
 
template<class StringTypeN >
static bool CutString (const StringTypeN &strtocut, const StringTypeN &separator, std::vector< StringTypeN > &vec)
 
template<class StringTypeN >
static int CountOccuranceInString (const StringTypeN &data, const StringTypeN &lookfor)
 
template<class StringTypeN >
static StringTypeN Replace (const StringTypeN &data, const StringTypeN &toreplace, const StringTypeN &replacer)
 
template<class StringTypeN >
static StringTypeN ReplaceSingleCharacter (const StringTypeN &data, const StringTypeN &toreplace, int replacer=' ')
 
template<class StringTypeN >
static StringTypeN RemoveCharacters (const StringTypeN &data, const StringTypeN &toremove)
 
template<class StringTypeN >
static StringTypeN RemoveFirstWords (const StringTypeN &data, int amount)
 
template<class StringTypeN >
static StringTypeN StitchTogether (const std::vector< StringTypeN *> &data, const StringTypeN &separator)
 
template<class StringTypeN >
static StringTypeN StitchTogether (const std::vector< StringTypeN > &data, const StringTypeN &separator)
 
template<class StringTypeN >
static StringTypeN StitchTogether (const std::vector< std::shared_ptr< StringTypeN >> &data, const StringTypeN &separator)
 
template<class StringTypeN >
static void RemovePreceedingTrailingSpaces (StringTypeN &str)
 
template<class StringTypeN >
static bool CompareInsensitive (const StringTypeN &data, const StringTypeN &second)
 
template<class StringTypeN >
static bool StringStartsWith (const StringTypeN &data, const StringTypeN &tomatch)
 
template<class StringTypeN >
static bool IsStringNumeric (const StringTypeN &data)
 
template<class StringTypeN >
static StringTypeN ToUpperCase (const StringTypeN &data)
 
template<class StringTypeN >
static StringTypeN Indent (size_t numspaces)
 
template<class StringTypeN >
static StringTypeN IndentLines (const StringTypeN &str, size_t spaces)
 Appends spaces number of spaces to each line in str and returns the result. More...
 
template<class StringTypeN >
static StringTypeN RemoveEnding (const StringTypeN &str, const StringTypeN &ending)
 
static bool IsLineTerminator (int32_t codepoint)
 
static bool IsLineTerminator (int32_t codepoint1, int32_t codepoint2)
 

Detailed Description

Singleton class that has string processing functions.

Most functions work with any type of string, but it is recommended to only pass string or wstring to avoid headaches.

Todo:

Get rid of ElementType and just use ints for everything and hope that it is large enough

Drop wstring support

Definition at line 33 of file StringOperations.h.

Member Function Documentation

◆ BaseHostName()

DLLEXPORT std::string StringOperations::BaseHostName ( const std::string &  url)
static

Definition at line 28 of file StringOperations.cpp.

28  {
29 
30  if(url.empty())
31  return "";
32 
33  // Start scanning until a '/' is found that is not preceeded by ':' or '/'
34  size_t length = 0;
35 
36  for(size_t i = 0; i < url.size(); ++i){
37 
38  length = i + 1;
39 
40  if(url[i] == '/'){
41 
42  if(i < 1)
43  continue;
44 
45  if(url[i - 1] == ':' || url[i - 1] == '/')
46  continue;
47 
48  // found it //
49  break;
50  }
51  }
52 
53  // Make sure it has an ending '/'
54  if(length == url.size() && url.back() != '/')
55  return url + "/";
56 
57  return url.substr(0, length);
58 }

◆ ChangeExtension()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::ChangeExtension ( const StringTypeN &  filepath,
const StringTypeN &  newext 
)
inlinestatic

Definition at line 145 of file StringOperations.h.

147  {
148  size_t startcopy = 0;
149  size_t endcopy = filepath.size()-1;
150 
151  size_t lastdot = filepath.find_last_of(DOT_CHARACTER);
152 
153  if(lastdot != StringTypeN::npos){
154  // dot found //
155  endcopy = lastdot;
156 
157  } else {
158  // No dot, so just append it //
159  return filepath+newext;
160  }
161 
162  // Return empty if no data is valid //
163  if(startcopy > endcopy || startcopy >= filepath.size() || endcopy >= filepath.size())
164  return StringTypeN();
165 
166  // Return the extension //
167  return filepath.substr(startcopy, endcopy-startcopy+1)+newext;
168  }
constexpr int32_t DOT_CHARACTER

◆ ChangeLineEndsToUniversal()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::ChangeLineEndsToUniversal ( const StringTypeN &  input)
inlinestatic

Changes all line separators to universal line separators.

Definition at line 316 of file StringOperations.h.

316  {
317 
318  StringTypeN results;
319 
320  // This is the line ending sequence //
321  StringTypeN separator;
323  sizeof(UNIVERSAL_LINE_SEPARATOR));
324 
325  // Try to find path strings and replace them //
326  size_t copystart = 0;
327  size_t copyend = 0;
328 
329  for(size_t i = 0; i < input.size(); i++){
330  if(input[i] == WINDOWS_LINE_SEPARATOR[0] &&
331  i+1 < input.size() && input[i+1] == WINDOWS_LINE_SEPARATOR[1])
332  {
333  // Found a line separator //
334  // Copy the current thing //
335  if(copyend >= copystart && copystart-copyend > 1)
336  results += input.substr(copystart, copyend-copystart+1);
337 
338 
339  results += separator;
340 
341  copystart = i+2 < input.size() ? i+2: i;
342  copyend = copystart;
343 
344  i += 2;
345 
346  continue;
347  }
348 
349  // Change the end copy //
350  copyend = i;
351  }
352 
353  if(copyend >= copystart && copystart-copyend > 1)
354  results += input.substr(copystart, copyend-copystart+1);
355 
356 
357  return results;
358  }
constexpr char UNIVERSAL_LINE_SEPARATOR[]
constexpr char WINDOWS_LINE_SEPARATOR[]
static void MakeString(StringTypeN &str, const char *characters, size_t count)

◆ ChangeLineEndsToWindows()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::ChangeLineEndsToWindows ( const StringTypeN &  input)
inlinestatic

Changes all line separators to Windows line separators.

Definition at line 271 of file StringOperations.h.

271  {
272 
273  StringTypeN results;
274 
275  // This is the line ending sequence //
276  StringTypeN separator;
278  sizeof(WINDOWS_LINE_SEPARATOR));
279 
280  // Try to find path strings and replace them //
281  StartEndIndex copyparts;
282 
283  for(size_t i = 0; i < input.size(); i++){
284  if(input[i] == UNIVERSAL_LINE_SEPARATOR[0]
285  // Previous character wasn't the first character of a windows line separator.
286  // If it was this is already the correct line separator and should be ignored
287  && (i == 0 || input[i - 1] != WINDOWS_LINE_SEPARATOR[0]))
288  {
289  // Found a line separator //
290  // Copy the current thing //
291  if(copyparts.Start && copyparts.End)
292  results += input.substr(copyparts.Start, copyparts.Length());
293 
294  copyparts.Reset();
295 
296  results += separator;
297 
298  continue;
299  }
300 
301  if (!copyparts.Start)
302  copyparts.Start = i;
303 
304  // Change the end copy //
305  copyparts.End = i;
306  }
307 
308  if (copyparts.End && copyparts.Start)
309  results += input.substr(copyparts.Start, copyparts.Length());
310 
311  return results;
312  }
constexpr char UNIVERSAL_LINE_SEPARATOR[]
constexpr char WINDOWS_LINE_SEPARATOR[]
static void MakeString(StringTypeN &str, const char *characters, size_t count)

◆ CombineURL()

DLLEXPORT std::string StringOperations::CombineURL ( const std::string &  first,
const std::string &  second 
)
static

Definition at line 91 of file StringOperations.cpp.

93 {
94  // To fix messed up urls we always do this cleanup
95  const auto cleanedUpSecond = RemovePartsBeforeAbsoluteURLParts(second);
96 
97  if(first.empty())
98  return cleanedUpSecond;
99 
100  if(cleanedUpSecond.empty())
101  return first;
102 
103  // If second is an absolute URL just return it //
104  if(cleanedUpSecond.find("://") != std::string::npos)
105  return cleanedUpSecond;
106 
107  // Simplest case: first ends with '/' and second doesn't begin with '/'
108  if(first.back() == '/' && cleanedUpSecond.front() != '/')
109  return first + cleanedUpSecond;
110 
111  // Second begins with '/': trim the first to the base url and then append the second
112  if(cleanedUpSecond.front() == '/')
113  return BaseHostName(first) + cleanedUpSecond.substr(1);
114 
115  // An error catching function
116  // If first is the basehostname then just combine them
117  if(first.back() != '/' && BaseHostName(first).length() == first.length() + 1)
118  return first + "/" + cleanedUpSecond;
119 
120  // Most complex case: trim from the end of first until the last '/' and then append second
121  const auto lastpos = first.find_last_of('/');
122  return first.substr(0, lastpos + 1) + cleanedUpSecond;
123 }
static DLLEXPORT std::string BaseHostName(const std::string &url)
static DLLEXPORT std::string RemovePartsBeforeAbsoluteURLParts(const std::string &url)

◆ CompareInsensitive()

template<class StringTypeN >
static bool Leviathan::StringOperations::CompareInsensitive ( const StringTypeN &  data,
const StringTypeN &  second 
)
inlinestatic

Definition at line 864 of file StringOperations.h.

866  {
867  if(data.size() != second.size())
868  return false;
869 
870  for(unsigned int i = 0; i < data.size(); i++){
871  if(data[i] != second[i]){
872 
873  // Check are they different case //
874  if(97 <= data[i] && data[i] <= 122){
875 
876  if(data[i]-32 != second[i]){
877 
878  return false;
879  }
880  } else if(97 <= second[i] && second[i] <= 122){
881 
882  if(second[i]-32 != data[i]){
883 
884  return false;
885  }
886  } else {
887 
888  return false;
889  }
890  }
891  }
892 
893  return true;
894  }

◆ CountOccuranceInString()

template<class StringTypeN >
static int Leviathan::StringOperations::CountOccuranceInString ( const StringTypeN &  data,
const StringTypeN &  lookfor 
)
inlinestatic

Definition at line 507 of file StringOperations.h.

509  {
510 
511  int count = 0;
512 
513  for(size_t i = 0; i < data.length(); i++){
514  if(data[i] == lookfor[0]){
515  // Found a possible match //
516  // test further //
517  size_t modifier = 0;
518  bool WasMatch = false;
519  while(data[i+modifier] == lookfor[modifier]){
520  // check can it increase without going out of bounds //
521  if((data.length() > i+modifier+1) && (lookfor.length() > modifier+1)){
522  // increase modifier to move forward //
523  modifier++;
524  } else {
525  // check is it a match
526  if(modifier+1 == lookfor.length()){
527  // found match! //
528  count++;
529  WasMatch = true;
530  break;
531  }
532  break;
533  }
534  }
535  // skip the separator amount of characters, if it was found //
536  if(WasMatch)
537  // -1 here so that first character of next string won't be missing,
538  // because of the loop incrementation
539  i += lookfor.length()-1;
540  }
541  }
542 
543  return count;
544  }

◆ CutLines()

template<class StringTypeN >
static size_t Leviathan::StringOperations::CutLines ( const StringTypeN &  input,
std::vector< StringTypeN > &  output 
)
inlinestatic

Splits a string on line separators.

Definition at line 362 of file StringOperations.h.

363  {
364  if(input.empty())
365  return 0;
366 
367  size_t copystart = 0;
368  size_t copyend = 0;
369 
370  for(size_t i = 0; i < input.size(); i++){
371  // Check is at a line separator
372  bool windows = input[i] == WINDOWS_LINE_SEPARATOR[0] &&
373  i+1 < input.size() && input[i+1] == WINDOWS_LINE_SEPARATOR[1];
374 
375  if(windows || (input[i] == UNIVERSAL_LINE_SEPARATOR[0]))
376  {
377  // Check that previous character wasn't the beginning of
378  // a windows line separator. If it was this is already added
379  // and should be ignored
380  if(i > 0 && input[i - 1] == WINDOWS_LINE_SEPARATOR[0]){
381 
382  // Skip adding this
383  continue;
384  }
385 
386  // Found a line separator //
387  // Copy the current thing //
388  if(copyend >= copystart && copystart-copyend > 1){
389 
390  output.push_back(input.substr(copystart, copyend - copystart + 1));
391 
392  } else {
393  // There was an empty line //
394  output.push_back(StringTypeN());
395  }
396 
397  copystart = windows ? i + 2 : i + 1;
398  copyend = 0;
399 
400  continue;
401  }
402 
403  copyend = i;
404  }
405 
406  if(copyend >= copystart)
407  output.push_back(input.substr(copystart, copyend - copystart + 1));
408 
409  return output.size();
410  }
constexpr char UNIVERSAL_LINE_SEPARATOR[]
constexpr char WINDOWS_LINE_SEPARATOR[]

◆ CutString()

template<class StringTypeN >
static bool Leviathan::StringOperations::CutString ( const StringTypeN &  strtocut,
const StringTypeN &  separator,
std::vector< StringTypeN > &  vec 
)
inlinestatic

Definition at line 415 of file StringOperations.h.

417  {
418  // scan the input and gather positions for string copying //
419  std::vector<StartEndIndex> CopyOperations;
420  bool PositionStarted = false;
421 
422  for(size_t i = 0; i < strtocut.length(); i++){
423  if(!PositionStarted){
424  PositionStarted = true;
425  // add new position index //
426  CopyOperations.push_back(StartEndIndex(i));
427  }
428 
429  if(strtocut[i] == separator[0]){
430  // Found a possible match //
431  // test further //
432  size_t modifier = 0;
433  bool WasMatch = false;
434  while(strtocut[i+modifier] == separator[modifier]){
435  // check can it increase without going out of bounds //
436  if((strtocut.length() > i+modifier+1) && (separator.length() > modifier+1)){
437  // increase modifier to move forward //
438  modifier++;
439  } else {
440  // check is it a match
441  if(modifier+1 == separator.length()){
442  // found match! //
443 
444  // end old string to just before this position //
445  CopyOperations.back().End = i; /*-1;
446  not this because we would have to
447  add 1 in the copy phase anyway */
448 
449  PositionStarted = false;
450  // skip separator //
451  WasMatch = true;
452  break;
453  }
454  break;
455  }
456  }
457 
458  // skip the separator amount of characters, if it was found //
459  if(WasMatch)
460  // -1 here so that first character of next string won't be missing,
461  // because of the loop incrementation
462  i += separator.length()-1;
463  }
464  }
465 
466  // Return empty string if there is nothing here //
467  if(CopyOperations.empty()){
468 
469  vec.push_back(StringTypeN());
470  return false;
471  }
472 
473  // make sure final position has end //
474  if(!CopyOperations.back().End)
475  CopyOperations.back().End = strtocut.length();
476 
477  if(CopyOperations.size() < 2){
478 
479  vec.push_back(strtocut.substr(CopyOperations.front().Start,
480  static_cast<size_t>(CopyOperations.front().End) -
481  static_cast<size_t>(CopyOperations.front().Start)));
482 
483  // would be just one string, for legacy
484  // (actually we don't want caller to think it got cut) reasons we return nothing //
485  return false;
486  }
487 
488  // length-1 is not used here, because it would have to be added in copy phase to the
489  // substring length, and we didn't add that earlier...
490 
491  // make space //
492  vec.reserve(CopyOperations.size());
493 
494  // loop through positions and copy substrings to result vector //
495  for(size_t i = 0; i < CopyOperations.size(); i++){
496  // copy using std::wstring method for speed //
497  vec.push_back(strtocut.substr(CopyOperations[i].Start,
498  static_cast<size_t>(CopyOperations[i].End) -
499  static_cast<size_t>(CopyOperations[i].Start)));
500  }
501 
502  // cutting succeeded //
503  return true;
504  }

◆ GetExtension()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::GetExtension ( const StringTypeN &  filepath)
inlinestatic

Definition at line 123 of file StringOperations.h.

123  {
124  size_t startcopy = 0;
125  size_t endcopy = filepath.size()-1;
126 
127  size_t lastdot = filepath.find_last_of(DOT_CHARACTER);
128 
129  if(lastdot == StringTypeN::npos){
130  // no dot //
131  return StringTypeN();
132  }
133 
134  startcopy = lastdot+1;
135 
136  // Return empty if no data is valid //
137  if(startcopy > endcopy || startcopy >= filepath.size() || endcopy >= filepath.size())
138  return StringTypeN();
139 
140  // Return the extension //
141  return filepath.substr(startcopy, endcopy-startcopy+1);
142  }
constexpr int32_t DOT_CHARACTER

◆ GetPath()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::GetPath ( const StringTypeN &  filepath)
inlinestatic

Returns the path part of a path+filename.

Definition at line 203 of file StringOperations.h.

203  {
204  size_t startcopy = 0;
205  size_t endcopy = filepath.size()-1;
206 
207  // Find last path character //
208  size_t lastpath;
209  bool found = false;
210 
211  for(size_t i = 0; i < filepath.size(); i++){
212  if(filepath[i] == UNIVERSAL_PATHSEPARATOR || filepath[i] ==
214  {
215  // Currently last found path //
216  lastpath = i;
217  found = true;
218  }
219  }
220 
221  if(!found){
222  // Set start //
223  return StringTypeN();
224  }
225 
226  // Set up copy //
227  endcopy = lastpath;
228 
229 
230  // Return empty if no data is valid //
231  if(startcopy > endcopy || startcopy >= filepath.size() || endcopy >= filepath.size())
232  return StringTypeN();
233 
234 
235  // return the wanted part //
236  return filepath.substr(startcopy, endcopy-startcopy+1);
237  }
constexpr int32_t UNIVERSAL_PATHSEPARATOR
constexpr int32_t WINDOWS_PATHSEPARATOR

◆ Indent()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::Indent ( size_t  numspaces)
inlinestatic

Definition at line 945 of file StringOperations.h.

945  {
946 
947  if (!numspaces)
948  return StringTypeN();
949 
950  return StringTypeN(numspaces, (int)' ');
951  }

◆ IndentLines()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::IndentLines ( const StringTypeN &  str,
size_t  spaces 
)
inlinestatic

Appends spaces number of spaces to each line in str and returns the result.

Definition at line 955 of file StringOperations.h.

955  {
956 
957  const auto indentstr = Indent<StringTypeN>(spaces);
958 
959  StringTypeN result;
960  result.reserve(str.size());
961 
962  StartEndIndex currentcut;
963 
964  for (size_t i = 0; i < str.size(); ++i) {
965 
966  // Check for line change //
967  if (IsLineTerminator(str[i])) {
968 
969  result += indentstr;
970 
971  if(currentcut.Start)
972  result += str.substr(currentcut.Start,
973  i - static_cast<size_t>(currentcut.Start));
974 
975  result += "\n";
976  currentcut = StartEndIndex();
977 
978  // Multi character line terminator //
979  if (i + 1 < str.length() && IsLineTerminator(str[i], str[i + 1]))
980  ++i;
981  }
982 
983  if (!currentcut.Start && !IsCharacterWhitespace(str[i])) {
984 
985  // Started a line //
986  currentcut.Start = i;
987  }
988  }
989 
990  if (currentcut.Start) {
991 
992  currentcut.End = str.size();
993 
994  result += indentstr + str.substr(currentcut.Start, currentcut.Length());
995  }
996 
997  return result;
998  }
static bool IsLineTerminator(int32_t codepoint)
static bool IsCharacterWhitespace(CharType character)

◆ IsCharacterQuote()

template<typename CharType >
static bool Leviathan::StringOperations::IsCharacterQuote ( CharType  character)
inlinestatic

Definition at line 47 of file StringOperations.h.

47  {
48 
49  if(character == '"' || character == '\'')
50  return true;
51 
52  return false;
53  }

◆ IsCharacterWhitespace()

template<typename CharType >
static bool Leviathan::StringOperations::IsCharacterWhitespace ( CharType  character)
inlinestatic

Definition at line 36 of file StringOperations.h.

36  {
37  if((int)character <= 32)
38  return true;
39 
40  if (IsLineTerminator(character))
41  return true;
42 
43  return false;
44  }
static bool IsLineTerminator(int32_t codepoint)

◆ IsLineTerminator() [1/2]

static bool Leviathan::StringOperations::IsLineTerminator ( int32_t  codepoint)
inlinestatic
Returns
True if a character is a line terminating character

Definition at line 1012 of file StringOperations.h.

1012  {
1013 
1014  if (codepoint == '\r' || codepoint == '\n' ||
1015  // Unicode newlines //
1016  codepoint == 0x0085 || codepoint == 0x2028 || codepoint == 0x2029 ||
1017  codepoint == 0x000B || codepoint == 0x000C)
1018  {
1019  return true;
1020  }
1021 
1022  return false;
1023  }

◆ IsLineTerminator() [2/2]

static bool Leviathan::StringOperations::IsLineTerminator ( int32_t  codepoint1,
int32_t  codepoint2 
)
inlinestatic
Returns
True if two characters are a line terminating sequence

Definition at line 1026 of file StringOperations.h.

1026  {
1027 
1028  if (codepoint1 == '\r' && codepoint2 == '\n')
1029  {
1030  return true;
1031  }
1032 
1033  return false;
1034  }

◆ IsStringNumeric()

template<class StringTypeN >
static bool Leviathan::StringOperations::IsStringNumeric ( const StringTypeN &  data)
inlinestatic

Definition at line 905 of file StringOperations.h.

905  {
906  for(size_t i = 0; i < data.size(); i++){
907  if((data[i] < FIRST_NUMBER ||
908  data[i] > LAST_NUMBER) &&
909  data[i] != DASH_CHARACTER &&
910  data[i] != DOT_CHARACTER &&
911  data[i] != PLUS_SYMBOL)
912  {
913  return false;
914  }
915  }
916  return true;
917  }
constexpr int32_t LAST_NUMBER
constexpr int32_t DOT_CHARACTER
constexpr int32_t DASH_CHARACTER
constexpr int32_t PLUS_SYMBOL
constexpr int32_t FIRST_NUMBER

◆ IsURLDomain()

DLLEXPORT bool StringOperations::IsURLDomain ( const std::string &  str)
static

Returns true if string looks like a top level domain.

Definition at line 158 of file StringOperations.cpp.

158  {
159 
160  // Must have a dot
161  bool dotSeen = false;
162 
163  for(char c : str){
164  if(c == '.'){
165  dotSeen = true;
166  continue;
167  }
168 
169  if(c >= '0' && c <= '9')
170  continue;
171 
172  if(c >= 'A' && c <= 'Z')
173  continue;
174 
175  if(c >= 'a' && c <= 'z')
176  continue;
177 
178  return false;
179  }
180 
181  return dotSeen;
182 }

◆ MakeString() [1/2]

DLLEXPORT void Leviathan::StringOperations::MakeString ( std::wstring &  str,
const char *  characters,
size_t  count 
)

Definition at line 7 of file StringOperations.cpp.

9 {
10  // Skip copying null terminator
11  const size_t copysize = count - 1;
12  str.resize(copysize);
13 
14  for (size_t i = 0; i < copysize; ++i)
15  str[i] = (wchar_t)characters[i];
16 }

◆ MakeString() [2/2]

template<class StringTypeN >
static void Leviathan::StringOperations::MakeString ( StringTypeN &  str,
const char *  characters,
size_t  count 
)
inlinestatic

Definition at line 58 of file StringOperations.h.

58  {
59 
60  // Skip the null terminator //
61  str = StringTypeN(characters, count - 1);
62  }

◆ RemoveCharacters()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::RemoveCharacters ( const StringTypeN &  data,
const StringTypeN &  toremove 
)
inlinestatic

Definition at line 651 of file StringOperations.h.

653  {
654  StringTypeN out;
655  out.reserve(data.size());
656 
657  for(auto iter = data.begin(); iter != data.end(); ++iter){
658 
659  // Check does it contain //
660  bool ignore = false;
661 
662  for(auto iter2 = toremove.begin(); iter2 != toremove.end(); ++iter2){
663 
664  if((*iter2) == (*iter)){
665 
666  ignore = true;
667  break;
668  }
669  }
670 
671  if(ignore)
672  continue;
673 
674  out.push_back(*iter);
675  }
676 
677  return out;
678  }

◆ RemoveEnding()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::RemoveEnding ( const StringTypeN &  str,
const StringTypeN &  ending 
)
inlinestatic

Definition at line 1001 of file StringOperations.h.

1001  {
1002 
1003  const auto pos = str.rfind(ending);
1004 
1005  if(pos != StringTypeN::npos && str.length() - pos == ending.length())
1006  return str.substr(0, pos);
1007 
1008  return str;
1009  }

◆ RemoveExtension()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::RemoveExtension ( const StringTypeN &  filepath,
bool  delpath = true 
)
inlinestatic

Definition at line 79 of file StringOperations.h.

81  {
82  size_t startcopy = 0;
83  size_t endcopy;
84 
85  size_t lastdot = filepath.find_last_of(DOT_CHARACTER);
86 
87  if(lastdot == StringTypeN::npos){
88  // no dot //
89  endcopy = filepath.size()-1;
90  } else {
91  endcopy = lastdot-1;
92  }
93 
94  // Potentially erase from beginning //
95  if(delpath){
96  // Find last path character //
97  size_t lastpath = 0;
98 
99  for(size_t i = 0; i < filepath.size(); i++){
100  if(filepath[i] == UNIVERSAL_PATHSEPARATOR || filepath[i] ==
102  {
103  // Currently last found path //
104  lastpath = i;
105  }
106  }
107 
108  if(lastpath != 0){
109  // Set start //
110  startcopy = lastpath+1;
111  }
112  }
113 
114  // Return empty if no data is valid //
115  if(startcopy > endcopy || startcopy >= filepath.size() || endcopy >= filepath.size())
116  return StringTypeN();
117 
118  // return the wanted part //
119  return filepath.substr(startcopy, endcopy-startcopy+1);
120  }
constexpr int32_t UNIVERSAL_PATHSEPARATOR
constexpr int32_t WINDOWS_PATHSEPARATOR
constexpr int32_t DOT_CHARACTER

◆ RemoveFirstWords()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::RemoveFirstWords ( const StringTypeN &  data,
int  amount 
)
inlinestatic

Definition at line 681 of file StringOperations.h.

681  {
682 
683  size_t firstpos = 0;
684  // Find the copy start position //
685  int spaces = 0;
686  int words = 0;
687 
688  for(size_t i = 0; i < data.length(); i++){
689  if(data[i] == SPACE_CHARACTER){
690  spaces++;
691  continue;
692  }
693  if(spaces > 0){
694  words++;
695  if(words == amount){
696  // This is the spot we want to start from //
697  firstpos = i;
698  break;
699  }
700  spaces = 0;
701  }
702  }
703 
704  if(firstpos == 0){
705  // Didn't remove anything? //
706  return data;
707  }
708 
709  // Generate sub string from start to end //
710  return data.substr(firstpos, data.size()-firstpos);
711  }
constexpr int32_t SPACE_CHARACTER

◆ RemovePartsBeforeAbsoluteURLParts()

DLLEXPORT std::string StringOperations::RemovePartsBeforeAbsoluteURLParts ( const std::string &  url)
static

Definition at line 126 of file StringOperations.cpp.

128 {
129  // Detect two '//'s in a path
130  const auto colonPos = url.find_first_of(':');
131 
132  if(colonPos != std::string::npos){
133 
134  // First double slash
135  auto firstDouble = url.find("//", colonPos + 3);
136  // Second
137  const auto secondDouble = url.find("//", firstDouble + 2);
138 
139  if(firstDouble != std::string::npos && secondDouble != std::string::npos){
140 
141  // If the part between the double slashes looks like a
142  // domain then we cut the part between the protocol and
143  // the first double slash
144  firstDouble += 2;
145 
146  if(IsURLDomain(url.substr(firstDouble, secondDouble - firstDouble))){
147 
148  return URLProtocol(url) + "://" +
149  url.substr(firstDouble, secondDouble - firstDouble) + "/" +
150  url.substr(secondDouble + 2);
151  }
152  }
153  }
154 
155  return url;
156 }
static DLLEXPORT std::string URLProtocol(const std::string &url)
static DLLEXPORT bool IsURLDomain(const std::string &str)
Returns true if string looks like a top level domain.

◆ RemovePath()

template<class StringTypeN >
static const StringTypeN Leviathan::StringOperations::RemovePath ( const StringTypeN &  filepath)
inlinestatic

Definition at line 171 of file StringOperations.h.

171  {
172  size_t startcopy = 0;
173  size_t endcopy = filepath.size()-1;
174 
175  // Find last path character //
176  size_t lastpath = 0;
177 
178  for(size_t i = 0; i < filepath.size(); i++){
179  if(filepath[i] == UNIVERSAL_PATHSEPARATOR
180  || filepath[i] == WINDOWS_PATHSEPARATOR)
181  {
182  // Currently last found path //
183  lastpath = i;
184  }
185  }
186 
187  if(lastpath != 0){
188  // Set start //
189  startcopy = lastpath+1;
190  }
191 
192  // Return empty if no data is valid //
193  if(startcopy > endcopy || startcopy >= filepath.size() || endcopy >= filepath.size())
194  return StringTypeN();
195 
196 
197  // return the wanted part //
198  return filepath.substr(startcopy, endcopy-startcopy+1);
199  }
constexpr int32_t UNIVERSAL_PATHSEPARATOR
constexpr int32_t WINDOWS_PATHSEPARATOR

◆ RemovePreceedingTrailingSpaces()

template<class StringTypeN >
static void Leviathan::StringOperations::RemovePreceedingTrailingSpaces ( StringTypeN &  str)
inlinestatic

Definition at line 805 of file StringOperations.h.

805  {
806  StartEndIndex CutPositions;
807 
808  // search the right part of the string //
809  for(size_t i = 0; i < str.size(); i++){
810  if(!IsCharacterWhitespace(str[i])){
811  if(!CutPositions.Start){
812  // beginning ended //
813  CutPositions.Start = i;
814  } else {
815  // set last pos as this //
816 
817  }
818  continue;
819  }
820  if(!CutPositions.Start){
821  // still haven't found a character //
822  continue;
823  }
824  // check is this last character //
825  size_t a = str.size()-1;
826  bool found = false;
827  for(; a > i; a--){
828  if(!IsCharacterWhitespace(str[a])){
829  // there is still valid characters //
830  found = true;
831  break;
832  }
833  }
834  if(found){
835  // skip to the found non-space character //
836  i = a-1;
837  continue;
838  }
839  // end found //
840  CutPositions.End = i-1;
841  break;
842  }
843 
844  if(!CutPositions.Start){
845  // nothing in the string //
846  str.clear();
847  return;
848  }
849  if(!CutPositions.End){
850  if(!CutPositions.Start){
851  // just the first character required //
852  CutPositions.End = CutPositions.Start;
853  } else {
854  // no need to cut from the end //
855  CutPositions.End = str.length()-1;
856  }
857  }
858 
859  // set the wstring as it's sub string //
860  str = str.substr(CutPositions.Start, CutPositions.Length());
861  }
static bool IsCharacterWhitespace(CharType character)

◆ RepeatCharacter()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::RepeatCharacter ( int  character,
size_t  count 
)
inlinestatic

Definition at line 65 of file StringOperations.h.

65  {
66 
67  StringTypeN result;
68 
69  result.resize(count);
70 
71  for(size_t i = 0; i < count; ++i)
72  result[i] = character;
73 
74  return result;
75  }

◆ Replace()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::Replace ( const StringTypeN &  data,
const StringTypeN &  toreplace,
const StringTypeN &  replacer 
)
inlinestatic

Definition at line 547 of file StringOperations.h.

549  {
550  // We construct an output string from the wanted bits //
551  StringTypeN out;
552 
553  if(toreplace.size() < 1){
554  // Don't replace anything //
555  return data;
556  }
557 
558  PotentiallySetIndex copystart;
559  PotentiallySetIndex copyend;
560 
561  // loop through data and copy final characters to out string //
562  for(size_t i = 0; i < data.size(); i++){
563  // check for replaced part //
564  if(data[i] == toreplace[0]){
565  // check for match //
566  bool IsMatch = false;
567  for(size_t checkind = 0; (checkind < toreplace.size()) &&
568  (checkind < data.size()); checkind++)
569  {
570  if(data[i+checkind] != toreplace[checkind]){
571  // didn't match //
572  break;
573  }
574  // check is final iteration //
575  if(!((checkind+1 < toreplace.size()) && (checkind+1 < data.size()))){
576  // is a match //
577  IsMatch = true;
578  break;
579  }
580  }
581 
582  if(IsMatch || toreplace.size() == 1){
583 
584  if(copystart && !copyend)
585  copyend = copystart;
586 
587  // First add proper characters //
588  if(copystart && copyend)
589  out += data.substr(copystart,
590  static_cast<size_t>(copyend) - static_cast<size_t>(copystart) + 1);
591 
592  copystart.ValueSet = false;
593  copyend.ValueSet = false;
594 
595  // it is a match, copy everything in replacer and
596  // add toreplace length to i
597  out += replacer;
598 
599  i += toreplace.length()-1;
600  continue;
601  }
602  }
603 
604  // non matching character mark as to copy //
605  if(!copystart){
606  copystart = i;
607  } else {
608  copyend = i;
609  }
610  }
611 
612  // Copy rest to out //
613  if(copystart && copyend)
614  out += data.substr(copystart,
615  static_cast<size_t>(copyend) - static_cast<size_t>(copystart) + 1);
616 
617  // Return finished string //
618  return out;
619  }

◆ ReplaceSingleCharacter()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::ReplaceSingleCharacter ( const StringTypeN &  data,
const StringTypeN &  toreplace,
int  replacer = ' ' 
)
inlinestatic

Definition at line 623 of file StringOperations.h.

625  {
626  // Copy the string and then modify it //
627  StringTypeN out(data);
628 
629  for(auto iter = out.begin(); iter != out.end(); ++iter){
630 
631  // Check does it contain //
632  bool replace = false;
633 
634  for(auto iter2 = toreplace.begin(); iter2 != toreplace.end(); ++iter2){
635 
636  if((*iter2) == (*iter)){
637 
638  replace = true;
639  break;
640  }
641  }
642 
643  if(replace)
644  (*iter) = replacer;
645  }
646 
647  return out;
648  }

◆ StitchTogether() [1/3]

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::StitchTogether ( const std::vector< StringTypeN *> &  data,
const StringTypeN &  separator 
)
inlinestatic

Definition at line 714 of file StringOperations.h.

716  {
717  StringTypeN ret;
718  bool first = true;
719  // reserve space //
720  int totalcharacters = 0;
721 
722  // This might be faster than not reserving space //
723  for(size_t i = 0; i < data.size(); i++){
724  totalcharacters += data[i]->length();
725  }
726 
727  totalcharacters += separator.length()*data.size();
728 
729  // By reserving space we don't have to allocate more memory
730  // during copying which might be faster
731  ret.reserve(totalcharacters);
732 
733  for(size_t i = 0; i < data.size(); i++){
734  if(!first)
735  ret += separator;
736  ret += *data[i];
737  first = false;
738  }
739 
740  return ret;
741  }

◆ StitchTogether() [2/3]

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::StitchTogether ( const std::vector< StringTypeN > &  data,
const StringTypeN &  separator 
)
inlinestatic

Definition at line 744 of file StringOperations.h.

746  {
747  StringTypeN ret;
748  bool first = true;
749  // reserve space //
750  int totalcharacters = 0;
751 
752  // This might be faster than not reserving space //
753  for(size_t i = 0; i < data.size(); i++){
754  totalcharacters += data[i].length();
755  }
756 
757  totalcharacters += separator.length()*data.size();
758 
759  // By reserving space we don't have to allocate more memory
760  // during copying which might be faster
761  ret.reserve(totalcharacters);
762 
763  for(size_t i = 0; i < data.size(); i++){
764  if(!first)
765  ret += separator;
766  ret += data[i];
767  first = false;
768  }
769 
770  return ret;
771  }

◆ StitchTogether() [3/3]

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::StitchTogether ( const std::vector< std::shared_ptr< StringTypeN >> &  data,
const StringTypeN &  separator 
)
inlinestatic

Definition at line 775 of file StringOperations.h.

778  {
779  StringTypeN ret;
780  bool first = true;
781  // reserve space //
782  int totalcharacters = 0;
783 
784  // This might be faster than not reserving space //
785  for(size_t i = 0; i < data.size(); i++){
786  totalcharacters += data[i]->length();
787  }
788  totalcharacters += separator.length()*data.size();
789 
790  // By reserving space we don't have to allocate more memory during
791  // copying which might be faster
792  ret.reserve(totalcharacters);
793 
794  for(size_t i = 0; i < data.size(); i++){
795  if(!first)
796  ret += separator;
797  ret += *data[i].get();
798  first = false;
799  }
800 
801  return ret;
802  }

◆ StringStartsWith()

template<class StringTypeN >
static bool Leviathan::StringOperations::StringStartsWith ( const StringTypeN &  data,
const StringTypeN &  tomatch 
)
inlinestatic

Definition at line 897 of file StringOperations.h.

899  {
900  size_t foundstop = data.find(tomatch);
901  return foundstop != StringTypeN::npos && foundstop == 0;
902  }

◆ ToUpperCase()

template<class StringTypeN >
static StringTypeN Leviathan::StringOperations::ToUpperCase ( const StringTypeN &  data)
inlinestatic
Todo:
Make this work with any unicode characters

Definition at line 921 of file StringOperations.h.

921  {
922 
923  StringTypeN result;
924  result.reserve(data.size());
925 
926  for(size_t i = 0; i < data.size(); i++){
927 
928  // Not actually unicode decoding...
929  auto const codepoint = data[i];
930 
931  if(97 <= codepoint && codepoint <= 122){
932 
933  result.push_back(codepoint-32);
934 
935  } else {
936 
937  result.push_back(codepoint);
938  }
939  }
940 
941  return result;
942  }

◆ URLPath()

DLLEXPORT std::string StringOperations::URLPath ( const std::string &  url)
static

Definition at line 60 of file StringOperations.cpp.

60  {
61 
62  if(url.empty())
63  return "";
64 
65  // Start scanning until a '/' is found that is not preceeded by ':' or '/'
66  size_t startCopy = 0;
67 
68  for(size_t i = 0; i < url.size(); ++i){
69 
70  if(url[i] == '/'){
71 
72  if(i < 1)
73  continue;
74 
75  if(url[i - 1] == ':' || url[i - 1] == '/')
76  continue;
77 
78  // found it //
79  startCopy = i + 1;
80  break;
81  }
82  }
83 
84  // Make sure the string doesn't end there
85  if(startCopy >= url.size())
86  return "";
87 
88  return url.substr(startCopy, url.size() - startCopy);
89 }

◆ URLProtocol()

DLLEXPORT std::string StringOperations::URLProtocol ( const std::string &  url)
static

Definition at line 18 of file StringOperations.cpp.

18  {
19 
20  const auto colonpos = url.find_first_of(':');
21 
22  if(colonpos == std::string::npos)
23  return "";
24 
25  return url.substr(0, colonpos);
26 }

The documentation for this class was generated from the following files: