string.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include "glib.h"
  3. /**
  4. * @brief 拷贝整个字符串
  5. *
  6. * @param dst 目标地址
  7. * @param src 源地址
  8. * @return char* 目标字符串
  9. */
  10. char *strcpy(char *dst, const char *src);
  11. //计算字符串的长度(经过测试,该版本比采用repne/scasb汇编的运行速度快16.8%左右)
  12. static inline int strlen(const char *s)
  13. {
  14. if (s == NULL)
  15. return 0;
  16. register int __res = 0;
  17. while (s[__res] != '\0')
  18. {
  19. ++__res;
  20. }
  21. return __res;
  22. }
  23. /**
  24. * @brief 测量字符串的长度
  25. *
  26. * @param src 字符串
  27. * @param maxlen 最大长度
  28. * @return long
  29. */
  30. long strnlen(const char *src, unsigned long maxlen);
  31. /*
  32. 比较字符串 FirstPart and SecondPart
  33. FirstPart = SecondPart => 0
  34. FirstPart > SecondPart => 1
  35. FirstPart < SecondPart => -1
  36. */
  37. int strcmp(const char *FirstPart, const char *SecondPart);
  38. char *strncpy(char *dst, const char *src, long count);
  39. long strncpy_from_user(char *dst, const char *src, unsigned long size);
  40. /**
  41. * @brief 测量来自用户空间的字符串的长度,会检验地址空间是否属于用户空间
  42. * @param src
  43. * @param maxlen
  44. * @return long
  45. */
  46. long strnlen_user(const char *src, unsigned long maxlen);
  47. /**
  48. * @brief 逐字节比较指定内存区域的值,并返回s1、s2的第一个不相等的字节i处的差值(s1[i]-s2[i])。
  49. * 若两块内存区域的内容相同,则返回0
  50. *
  51. * @param s1 内存区域1
  52. * @param s2 内存区域2
  53. * @param len 要比较的内存区域长度
  54. * @return int s1、s2的第一个不相等的字节i处的差值(s1[i]-s2[i])。若两块内存区域的内容相同,则返回0
  55. */
  56. static inline int memcmp(const void *s1, const void *s2, size_t len)
  57. {
  58. int diff;
  59. asm("cld \n\t" // 复位DF,确保s1、s2指针是自增的
  60. "repz; cmpsb\n\t" CC_SET(nz)
  61. : CC_OUT(nz)(diff), "+D"(s1), "+S"(s2)
  62. : "c"(len)
  63. : "memory");
  64. if (diff)
  65. diff = *(const unsigned char *)(s1 - 1) - *(const unsigned char *)(s2 - 1);
  66. return diff;
  67. }