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