Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
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.
 
 
 
 

110 lines
2.8 KiB

  1. #include "str.h"
  2. #include "buffer.h"
  3. /*
  4. * The str_* functions have a pretty similar functionality to the byte_* ones
  5. * -- the most important enhacement is the recognition of \0.
  6. */
  7. int
  8. main()
  9. {
  10. char *string1 = "foo bar";
  11. char string2[42] = "fnord";
  12. unsigned int len;
  13. unsigned int amm;
  14. unsigned int pos;
  15. int diff;
  16. int start;
  17. /* For determinating the difference between two strings, thew libowfat
  18. * offers two functions:
  19. *
  20. * int str_diff(const char *a,const char *b);
  21. *
  22. * and
  23. *
  24. * int str_diffn(const char *a,const char *b,unsigned int limit);
  25. *
  26. * They both return <0 if a is lexicographically smaller than b, 0 if they
  27. * are equal and >0 if b is greater than a. The only difference is that
  28. * str_diff compares everytime until a \0 is encountered and str_diffn
  29. * compares max. until limit. Beware of checking just for 1 and -1!
  30. */
  31. diff = str_diff(string1, string2);
  32. buffer_puts(buffer_1, "str_diff(string1, string2): ");
  33. buffer_putlong(buffer_1, diff);
  34. buffer_putnlflush(buffer_1);
  35. /* As in byte_diff(), there is a macro str_equal(a,b) for convience. */
  36. /* For the frequent task of testing whether a string starts with an another
  37. * is the following function:
  38. *
  39. * int str_start(const char *a,const char *b);
  40. *
  41. * It simply returns 1 if a starts with b and 0 otherwise.
  42. */
  43. start = str_start(string1, "foo");
  44. buffer_puts(buffer_1, "str_start(string1, \"foo\"): ");
  45. buffer_putlong(buffer_1, start);
  46. buffer_putnlflush(buffer_1);
  47. /* Determinating the length of a string is also easy with the libowfat:
  48. *
  49. * unsigned int str_len(const char *s);
  50. *
  51. * returns the index of \0 in the string and that's also the length.
  52. */
  53. len = str_len(string1);
  54. buffer_puts(buffer_1, "str_len(buffer_1): ");
  55. buffer_putulong(buffer_1, len);
  56. buffer_putnlflush(buffer_1);
  57. /* One of the most used ANSI-C-functions is strcpy() and the libowfat has
  58. * an own version:
  59. *
  60. * unsigned int str_copy(char *out,const char *in);
  61. *
  62. * It copies "in" into "out" until the first \0 is copied and returns the
  63. * number of bytes copied.
  64. */
  65. amm = str_copy(string2, string1); /* Returns 7, string2 now begins with "foo bar\0". */
  66. buffer_puts(buffer_1, "str_copy(string1, string2): ");
  67. buffer_putulong(buffer_1, amm);
  68. buffer_putnlflush(buffer_1);
  69. /* Similar to byte_* there are:
  70. *
  71. * unsigned int str_chr(const char *haystack,char needle);
  72. *
  73. * and
  74. *
  75. * unsigned int str_rchr(const char *haystack,char needle);
  76. *
  77. * The first one returns the index of the _first_ occurency of "needle" in
  78. * "haystack" and the second returns the last one. Both return the index of
  79. * \0 if the "needle" hasn't been found.
  80. */
  81. pos = str_chr(string1, 'b'); /* Returns 4. */
  82. buffer_puts(buffer_1, "str_chr(string1, 'b'): ");
  83. buffer_putulong(buffer_1, pos);
  84. buffer_putnlflush(buffer_1);
  85. return 0;
  86. }