Contains standard libraries for ZenCode.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Strings.zs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. export expand string {
  2. public const in(c as char) as bool
  3. => indexOf(c) >= 0;
  4. public const indexOf(c as char) as int {
  5. for i in 0 .. length {
  6. if this[i] == c
  7. return i;
  8. }
  9. return -1;
  10. }
  11. public const indexOf(c as char, from as int) as int {
  12. for i in from .. length {
  13. if this[i] == c
  14. return i;
  15. }
  16. return -1;
  17. }
  18. public const lastIndexOf(c as char) as int {
  19. var i = length;
  20. while i > 0 {
  21. i--;
  22. if this[i] == c
  23. return i;
  24. }
  25. return -1;
  26. }
  27. public const lastIndexOf(c as char, until as int) as int {
  28. var i = until;
  29. while i > 0 {
  30. i--;
  31. if this[i] == c
  32. return i;
  33. }
  34. return -1;
  35. }
  36. public const split(delimiter as char) as string[] {
  37. val result = new List<string>();
  38. var start = 0;
  39. for i in 0 .. this.length {
  40. if this[i] == delimiter {
  41. result.add(this[start .. i]);
  42. start = i + 1;
  43. }
  44. }
  45. result.add(this[start .. $]);
  46. return result as string[];
  47. }
  48. public const trim() as string {
  49. var from = 0;
  50. while from < this.length && this[from] in [' ', '\t', '\r', '\n']
  51. from++;
  52. var to = this.length;
  53. while to > 0 && this[to - 1] in [' ', '\t', '\r', '\n']
  54. to--;
  55. return to < from ? "" : this[from .. to];
  56. }
  57. public const lpad(length as int, c as char) as string
  58. => this.length >= length ? this : c.times(length - this.length) + this;
  59. public const rpad(length as int, c as char) as string
  60. => this.length >= length ? this : this + c.times(length - this.length);
  61. }