string_intrinsics.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* String intrinsics helper functions.
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* Unlike what the name of this file suggests, we don't actually
  20. implement the Fortran intrinsics here. At least, not with the
  21. names they have in the standard. The functions here provide all
  22. the support we need for the standard string intrinsics, and the
  23. compiler translates the actual intrinsics calls to calls to
  24. functions in this file. */
  25. #include "libgfortran.h"
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. /* Helper function to set parts of wide strings to a constant (usually
  30. spaces). */
  31. static gfc_char4_t *
  32. memset_char4 (gfc_char4_t *b, gfc_char4_t c, size_t len)
  33. {
  34. size_t i;
  35. for (i = 0; i < len; i++)
  36. b[i] = c;
  37. return b;
  38. }
  39. /* Compare wide character types, which are handled internally as
  40. unsigned 4-byte integers. */
  41. int
  42. memcmp_char4 (const void *a, const void *b, size_t len)
  43. {
  44. const GFC_UINTEGER_4 *pa = a;
  45. const GFC_UINTEGER_4 *pb = b;
  46. while (len-- > 0)
  47. {
  48. if (*pa != *pb)
  49. return *pa < *pb ? -1 : 1;
  50. pa ++;
  51. pb ++;
  52. }
  53. return 0;
  54. }
  55. /* All other functions are defined using a few generic macros in
  56. string_intrinsics_inc.c, so we avoid code duplication between the
  57. various character type kinds. */
  58. #undef CHARTYPE
  59. #define CHARTYPE char
  60. #undef UCHARTYPE
  61. #define UCHARTYPE unsigned char
  62. #undef SUFFIX
  63. #define SUFFIX(x) x
  64. #undef MEMSET
  65. #define MEMSET memset
  66. #undef MEMCMP
  67. #define MEMCMP memcmp
  68. #include "string_intrinsics_inc.c"
  69. #undef CHARTYPE
  70. #define CHARTYPE gfc_char4_t
  71. #undef UCHARTYPE
  72. #define UCHARTYPE gfc_char4_t
  73. #undef SUFFIX
  74. #define SUFFIX(x) x ## _char4
  75. #undef MEMSET
  76. #define MEMSET memset_char4
  77. #undef MEMCMP
  78. #define MEMCMP memcmp_char4
  79. #include "string_intrinsics_inc.c"