base.stringhelper.bmx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ' Copyright (c) 2014-2021 Bruce A Henderson
  2. ' Copyright (c) 2014-2017 Ronny Otto
  3. '
  4. ' This software is provided 'as-is', without any express or implied
  5. ' warranty. In no event will the authors be held liable for any damages
  6. ' arising from the use of this software.
  7. '
  8. ' Permission is granted to anyone to use this software for any purpose,
  9. ' including commercial applications, and to alter it and redistribute it
  10. ' freely, subject to the following restrictions:
  11. '
  12. ' 1. The origin of this software must not be misrepresented; you must not
  13. ' claim that you wrote the original software. If you use this software
  14. ' in a product, an acknowledgment in the product documentation would be
  15. ' appreciated but is not required.
  16. '
  17. ' 2. Altered source versions must be plainly marked as such, and must not be
  18. ' misrepresented as being the original software.
  19. '
  20. ' 3. This notice may not be removed or altered from any source
  21. ' distribution.
  22. '
  23. SuperStrict
  24. Import "transform.c"
  25. Import "stringbuffer_core.bmx"
  26. Extern
  27. Function transform:Byte Ptr(i:Int)
  28. End Extern
  29. Type TStringHelper
  30. 'this function replaces non-alphanumerical characters with
  31. 'the string "replaceInvalidCharsWith"
  32. 'certain characters (German umlauts, French accents) are replaced
  33. 'with their basic characters (é = e)
  34. Function Sanitize:String(value:String, replaceInvalidCharsWith:String="_", requiresAlphaPrefix:Int = False)
  35. Local result:TStringBuffer = New TStringBuffer
  36. For Local i:Int = 0 Until value.length
  37. Local c:Byte Ptr = transform(value[i])
  38. 'append the char - or the replacement
  39. If c
  40. If Not i Then
  41. Local n:Int = c[0]
  42. If n >= Asc("0") And n <= Asc("9") Then
  43. result.Append(replaceInvalidCharsWith)
  44. End If
  45. End If
  46. result.AppendCString(c)
  47. Else
  48. result.Append(replaceInvalidCharsWith)
  49. EndIf
  50. Next
  51. Return result.ToString()
  52. End Function
  53. End Type