my_strchr.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. #include <stddef.h>
  23. #include <sys/types.h>
  24. #include "m_ctype.h"
  25. /**
  26. Calculate the length of the initial segment of 'str' which consists
  27. entirely of characters not in 'reject'.
  28. @param cs Pointer to charset info.
  29. @param str Pointer to multi-byte string.
  30. @param str_end Pointer to end of multi-byte string.
  31. @param reject Pointer to start of single-byte reject string.
  32. @param reject_length Length of single-byte reject string.
  33. @return Length of segment of multi-byte string that doesn't contain
  34. any character of the single-byte reject string or zero if an
  35. invalid encoding of a character of the multi-byte string is
  36. found.
  37. @note The reject string points to single-byte characters so it is
  38. only possible to find the first occurrence of a single-byte
  39. character. Multi-byte characters in 'str' are treated as not
  40. matching any character in the reject string.
  41. This method returns zero if an invalid encoding of any character
  42. in the string 'str' using charset 'cs' is found.
  43. @todo should be moved to CHARSET_INFO if it's going to be called
  44. frequently.
  45. @internal The implementation builds on the assumption that 'str' is long,
  46. while 'reject' is short. So it compares each character in string
  47. with the characters in 'reject' in a tight loop over the characters
  48. in 'reject'.
  49. */
  50. size_t my_strcspn(const CHARSET_INFO *cs, const char *str, const char *str_end,
  51. const char *reject, size_t reject_length) {
  52. const char *ptr_str, *ptr_reject;
  53. const char *reject_end = reject + reject_length;
  54. uint mbl = 0;
  55. for (ptr_str = str; ptr_str < str_end; ptr_str += mbl) {
  56. mbl = my_mbcharlen_ptr(cs, ptr_str, str_end);
  57. if (mbl == 0) return 0;
  58. if (mbl == 1) {
  59. for (ptr_reject = reject; ptr_reject < reject_end; ++ptr_reject) {
  60. if (*ptr_reject == *ptr_str) return (size_t)(ptr_str - str);
  61. }
  62. }
  63. }
  64. return (size_t)(ptr_str - str);
  65. }