/doc/zebra/zstr.c

https://github.com/jhallen/joes-sandbox · C · 127 lines · 93 code · 17 blank · 17 comment · 31 complexity · 737981cdd8c777fe30e152e87a69d555 MD5 · raw file

  1. /* Zero terminated strings
  2. Copyright (C) 1992 Joseph H. Allen
  3. This file is part of JOE (Joe's Own Editor)
  4. JOE is free software; you can redistribute it and/or modify it under the
  5. terms of the GNU General Public License as published by the Free Software
  6. Foundation; either version 1, or (at your option) any later version.
  7. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. details.
  11. You should have received a copy of the GNU General Public License along with
  12. JOE; see the file COPYING. If not, write to the Free Software Foundation,
  13. 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. #include <stdlib.h>
  15. #include "zstr.h"
  16. int toup(int a)
  17. {
  18. return (a >= 'a' && a <= 'z') ? a + 'A' - 'a' : a;
  19. }
  20. unsigned Umin(unsigned a, unsigned b)
  21. {
  22. return a < b ? a : b;
  23. }
  24. unsigned Umax(unsigned a, unsigned b)
  25. {
  26. return a > b ? a : b;
  27. }
  28. int Imin(int a, int b)
  29. {
  30. return a < b ? a : b;
  31. }
  32. int Imax(int a, int b)
  33. {
  34. return a > b ? a : b;
  35. }
  36. int Iabs(int a)
  37. {
  38. return a >= 0 ? a : -a;
  39. }
  40. int cword(int c)
  41. {
  42. return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0'
  43. && c <= '9' || c == '_';
  44. }
  45. int cwhitel(int c)
  46. {
  47. return c == ' ' || c == '\t' || c == '\n';
  48. }
  49. int cwhite(int c)
  50. {
  51. return c == ' ' || c == '\t';
  52. }
  53. int zlen(char *s)
  54. {
  55. char *os = s;
  56. while (*s)
  57. ++s;
  58. return s - os;
  59. }
  60. char *zcpy(char *d, char *s)
  61. {
  62. char *od = d;
  63. while (*d++ = *s++);
  64. return od;
  65. }
  66. char *zcat(char *d, char *s)
  67. {
  68. char *od = d;
  69. while (*d)
  70. ++d;
  71. while (*d++ = *s++);
  72. return od;
  73. }
  74. char *zdup(char *s)
  75. {
  76. return zcpy((char *) malloc(zlen(s) + 1), s);
  77. }
  78. int zcmp(char *l, char *r)
  79. {
  80. while (*l && *l == *r)
  81. ++l, ++r;
  82. if (*l > *r)
  83. return 1;
  84. if (*l < *r)
  85. return -1;
  86. return 0;
  87. }
  88. int fields(char *s, char **fields, int sep)
  89. {
  90. int y = 1;
  91. fields[0] = s;
  92. while (*s) {
  93. if (*s == sep)
  94. fields[y++] = s + 1, *s = 0;
  95. ++s;
  96. }
  97. return y;
  98. }
  99. int nfields(char *s, int sep)
  100. {
  101. int y = 1;
  102. while (*s)
  103. if (*s++ == sep)
  104. ++y;
  105. return y;
  106. }