examples_dstr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * File : examples_dstr.c
  3. * This file is the example code of dstr(dynamic string) package
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-06-07 never the first version
  23. */
  24. #include <rtthread.h>
  25. #include <finsh.h>
  26. #include "dstr.h"
  27. void rt_dstr_printf(rt_dstr_t *thiz)
  28. {
  29. if (thiz == NULL)
  30. return;
  31. rt_kprintf("%s\n", thiz->str);
  32. }
  33. void dstr_test_new(void)
  34. {
  35. rt_dstr_t *p = NULL;
  36. p = rt_dstr_new("new dstr");
  37. rt_dstr_printf(p);
  38. rt_dstr_del(p);
  39. }
  40. void dstr_test_cat(void)
  41. {
  42. rt_dstr_t *p = NULL;
  43. p = rt_dstr_new("cat");
  44. rt_dstr_cat(p, " dstr");
  45. rt_dstr_printf(p);
  46. rt_dstr_del(p);
  47. }
  48. void dstr_test_ncat(void)
  49. {
  50. rt_dstr_t *p1 = NULL;
  51. p1 = rt_dstr_new("ncat");
  52. rt_dstr_ncat(p1, " dstrnnn", 5);
  53. rt_dstr_ncat(p1, "1234", 3);
  54. rt_dstr_printf(p1);
  55. rt_kprintf("p2 str:%s\n",p1->str);
  56. rt_dstr_del(p1);
  57. }
  58. void dstr_test_cmp(void)
  59. {
  60. rt_dstr_t *p1 = NULL;
  61. rt_dstr_t *p2 = NULL;
  62. int res = 0;
  63. p1 = rt_dstr_new("helle");
  64. p2 = rt_dstr_new("hellc");
  65. res = rt_dstr_cmp(p1, p2);
  66. rt_kprintf("cmp result: %d\n", res);
  67. rt_dstr_del(p1);
  68. rt_dstr_del(p2);
  69. // NULL
  70. p1 = rt_dstr_new("abc");
  71. res = rt_dstr_cmp(p1, NULL);
  72. rt_kprintf("s2:NULL result: %d\n", res);
  73. rt_dstr_del(p1);
  74. p1 = rt_dstr_new("efg");
  75. res = rt_dstr_cmp(NULL, p1);
  76. rt_kprintf("s1:NULL result: %d\n", res);
  77. rt_dstr_del(p1);
  78. res = rt_dstr_cmp(NULL, NULL);
  79. rt_kprintf("two NULL result: %d\n", res);
  80. }
  81. void dstr_test_ncmp(void)
  82. {
  83. rt_dstr_t *p1 = NULL;
  84. rt_dstr_t *p2 = NULL;
  85. int res = 0;
  86. p1 = rt_dstr_new("hello");
  87. p2 = rt_dstr_new("hella");
  88. res = rt_dstr_ncmp(p1, p2, 5);
  89. rt_kprintf("ncmp result: %d\n", res);
  90. rt_dstr_del(p1);
  91. rt_dstr_del(p2);
  92. // NULL
  93. p1 = rt_dstr_new("abc");
  94. res = rt_dstr_ncmp(p1, NULL, 2);
  95. rt_kprintf("s2:NULL ncmp result: %d\n", res);
  96. rt_dstr_del(p1);
  97. p1 = rt_dstr_new("efg");
  98. res = rt_dstr_ncmp(NULL, p1, 3);
  99. rt_kprintf("s1:NULL ncmp result: %d\n", res);
  100. rt_dstr_del(p1);
  101. res = rt_dstr_ncmp(NULL, NULL, 4);
  102. rt_kprintf("two NULL ncmp result: %d\n", res);
  103. }
  104. void dstr_test_casecmp(void)
  105. {
  106. rt_dstr_t *p1 = NULL;
  107. rt_dstr_t *p2 = NULL;
  108. int res = 0;
  109. p1 = rt_dstr_new("hello");
  110. p2 = rt_dstr_new("HELLO");
  111. res = rt_dstr_casecmp(p1, p2);
  112. rt_kprintf("casecmp result: %d\n", res);
  113. rt_dstr_del(p1);
  114. rt_dstr_del(p2);
  115. // NULL
  116. p1 = rt_dstr_new("abc");
  117. res = rt_dstr_casecmp(p1, NULL);
  118. rt_kprintf("s2:NULL casecmp result: %d\n", res);
  119. rt_dstr_del(p1);
  120. p1 = rt_dstr_new("efg");
  121. res = rt_dstr_casecmp(NULL, p1);
  122. rt_kprintf("s1:NULL casecmp result: %d\n", res);
  123. rt_dstr_del(p1);
  124. res = rt_dstr_casecmp(NULL, NULL);
  125. rt_kprintf("two NULL casecmp result: %d\n", res);
  126. }
  127. void dstr_test_strlen(void)
  128. {
  129. rt_dstr_t *p1 = NULL;
  130. int res = 0;
  131. p1 = rt_dstr_new("hello strlen");
  132. res = rt_dstr_strlen(p1);
  133. if (res == -1)
  134. return;
  135. rt_kprintf("length: %d\n", res);
  136. rt_dstr_del(p1);
  137. }
  138. void dstr_test_sprintf(void)
  139. {
  140. const char *src = "test sprintf";
  141. rt_dstr_t *p1 = NULL;
  142. rt_dstr_t *p2 = NULL;
  143. // string format
  144. p1 = rt_dstr_new("");
  145. rt_dstr_sprintf(p1, "%s", src);
  146. rt_dstr_printf(p1);
  147. rt_dstr_del(p1);
  148. // hex format
  149. p2 = rt_dstr_new("");
  150. rt_dstr_sprintf(p2, "%08x", 0x20180604);
  151. rt_dstr_printf(p2);
  152. rt_dstr_del(p2);
  153. }
  154. void dstr_test(void)
  155. {
  156. dstr_test_new();
  157. dstr_test_cat();
  158. dstr_test_ncat();
  159. dstr_test_cmp();
  160. dstr_test_ncmp();
  161. dstr_test_casecmp();
  162. dstr_test_strlen();
  163. dstr_test_sprintf();
  164. }
  165. MSH_CMD_EXPORT(dstr_test, dstr example);