string_intrinsics_inc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* String intrinsics helper functions.
  2. Copyright (C) 2002-2022 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. /* Rename the functions. */
  20. #define concat_string SUFFIX(concat_string)
  21. #define string_len_trim SUFFIX(string_len_trim)
  22. #define adjustl SUFFIX(adjustl)
  23. #define adjustr SUFFIX(adjustr)
  24. #define string_index SUFFIX(string_index)
  25. #define string_scan SUFFIX(string_scan)
  26. #define string_verify SUFFIX(string_verify)
  27. #define string_trim SUFFIX(string_trim)
  28. #define string_minmax SUFFIX(string_minmax)
  29. #define zero_length_string SUFFIX(zero_length_string)
  30. #define compare_string SUFFIX(compare_string)
  31. /* The prototypes. */
  32. extern void concat_string (gfc_charlen_type, CHARTYPE *,
  33. gfc_charlen_type, const CHARTYPE *,
  34. gfc_charlen_type, const CHARTYPE *);
  35. export_proto(concat_string);
  36. extern void adjustl (CHARTYPE *, gfc_charlen_type, const CHARTYPE *);
  37. export_proto(adjustl);
  38. extern void adjustr (CHARTYPE *, gfc_charlen_type, const CHARTYPE *);
  39. export_proto(adjustr);
  40. extern gfc_charlen_type string_index (gfc_charlen_type, const CHARTYPE *,
  41. gfc_charlen_type, const CHARTYPE *,
  42. GFC_LOGICAL_4);
  43. export_proto(string_index);
  44. extern gfc_charlen_type string_scan (gfc_charlen_type, const CHARTYPE *,
  45. gfc_charlen_type, const CHARTYPE *,
  46. GFC_LOGICAL_4);
  47. export_proto(string_scan);
  48. extern gfc_charlen_type string_verify (gfc_charlen_type, const CHARTYPE *,
  49. gfc_charlen_type, const CHARTYPE *,
  50. GFC_LOGICAL_4);
  51. export_proto(string_verify);
  52. extern void string_trim (gfc_charlen_type *, CHARTYPE **, gfc_charlen_type,
  53. const CHARTYPE *);
  54. export_proto(string_trim);
  55. extern void string_minmax (gfc_charlen_type *, CHARTYPE **, int, int, ...);
  56. export_proto(string_minmax);
  57. /* Use for functions which can return a zero-length string. */
  58. static CHARTYPE zero_length_string = 0;
  59. /* Strings of unequal length are extended with pad characters. */
  60. int
  61. compare_string (gfc_charlen_type len1, const CHARTYPE *s1,
  62. gfc_charlen_type len2, const CHARTYPE *s2)
  63. {
  64. const UCHARTYPE *s;
  65. gfc_charlen_type len;
  66. int res;
  67. /* Placate the sanitizer. */
  68. if (!s1 && !s2)
  69. return 0;
  70. if (!s1)
  71. return -1;
  72. if (!s2)
  73. return 1;
  74. res = MEMCMP (s1, s2, ((len1 < len2) ? len1 : len2));
  75. if (res != 0)
  76. return res;
  77. if (len1 == len2)
  78. return 0;
  79. if (len1 < len2)
  80. {
  81. len = len2 - len1;
  82. s = (UCHARTYPE *) &s2[len1];
  83. res = -1;
  84. }
  85. else
  86. {
  87. len = len1 - len2;
  88. s = (UCHARTYPE *) &s1[len2];
  89. res = 1;
  90. }
  91. while (len--)
  92. {
  93. if (*s != ' ')
  94. {
  95. if (*s > ' ')
  96. return res;
  97. else
  98. return -res;
  99. }
  100. s++;
  101. }
  102. return 0;
  103. }
  104. iexport(compare_string);
  105. /* The destination and source should not overlap. */
  106. void
  107. concat_string (gfc_charlen_type destlen, CHARTYPE * dest,
  108. gfc_charlen_type len1, const CHARTYPE * s1,
  109. gfc_charlen_type len2, const CHARTYPE * s2)
  110. {
  111. if (len1 >= destlen)
  112. {
  113. memcpy (dest, s1, destlen * sizeof (CHARTYPE));
  114. return;
  115. }
  116. memcpy (dest, s1, len1 * sizeof (CHARTYPE));
  117. dest += len1;
  118. destlen -= len1;
  119. if (len2 >= destlen)
  120. {
  121. memcpy (dest, s2, destlen * sizeof (CHARTYPE));
  122. return;
  123. }
  124. memcpy (dest, s2, len2 * sizeof (CHARTYPE));
  125. MEMSET (&dest[len2], ' ', destlen - len2);
  126. }
  127. /* Return string with all trailing blanks removed. */
  128. void
  129. string_trim (gfc_charlen_type *len, CHARTYPE **dest, gfc_charlen_type slen,
  130. const CHARTYPE *src)
  131. {
  132. *len = string_len_trim (slen, src);
  133. if (*len == 0)
  134. *dest = &zero_length_string;
  135. else
  136. {
  137. /* Allocate space for result string. */
  138. *dest = xmallocarray (*len, sizeof (CHARTYPE));
  139. /* Copy string if necessary. */
  140. memcpy (*dest, src, *len * sizeof (CHARTYPE));
  141. }
  142. }
  143. /* The length of a string not including trailing blanks. */
  144. gfc_charlen_type
  145. string_len_trim (gfc_charlen_type len, const CHARTYPE *s)
  146. {
  147. if (len <= 0)
  148. return 0;
  149. const size_t long_len = sizeof (unsigned long);
  150. size_t i = len - 1;
  151. /* If we've got the standard (KIND=1) character type, we scan the string in
  152. long word chunks to speed it up (until a long word is hit that does not
  153. consist of ' 's). */
  154. if (sizeof (CHARTYPE) == 1 && i >= long_len)
  155. {
  156. size_t starting;
  157. unsigned long blank_longword;
  158. /* Handle the first characters until we're aligned on a long word
  159. boundary. Actually, s + i + 1 must be properly aligned, because
  160. s + i will be the last byte of a long word read. */
  161. starting = (
  162. #ifdef __INTPTR_TYPE__
  163. (__INTPTR_TYPE__)
  164. #endif
  165. (s + i + 1)) % long_len;
  166. i -= starting;
  167. for (; starting > 0; --starting)
  168. if (s[i + starting] != ' ')
  169. return i + starting + 1;
  170. /* Handle the others in a batch until first non-blank long word is
  171. found. Here again, s + i is the last byte of the current chunk,
  172. to it starts at s + i - sizeof (long) + 1. */
  173. #if __SIZEOF_LONG__ == 4
  174. blank_longword = 0x20202020L;
  175. #elif __SIZEOF_LONG__ == 8
  176. blank_longword = 0x2020202020202020L;
  177. #else
  178. #error Invalid size of long!
  179. #endif
  180. while (i >= long_len)
  181. {
  182. i -= long_len;
  183. if (*((unsigned long*) (s + i + 1)) != blank_longword)
  184. {
  185. i += long_len;
  186. break;
  187. }
  188. }
  189. }
  190. /* Simply look for the first non-blank character. */
  191. while (s[i] == ' ')
  192. {
  193. if (i == 0)
  194. return 0;
  195. --i;
  196. }
  197. return i + 1;
  198. }
  199. /* Find a substring within a string. */
  200. gfc_charlen_type
  201. string_index (gfc_charlen_type slen, const CHARTYPE *str,
  202. gfc_charlen_type sslen, const CHARTYPE *sstr,
  203. GFC_LOGICAL_4 back)
  204. {
  205. gfc_charlen_type start, last, delta, i;
  206. if (sslen == 0)
  207. return back ? (slen + 1) : 1;
  208. if (sslen > slen)
  209. return 0;
  210. if (!back)
  211. {
  212. last = slen + 1 - sslen;
  213. start = 0;
  214. delta = 1;
  215. }
  216. else
  217. {
  218. last = -1;
  219. start = slen - sslen;
  220. delta = -1;
  221. }
  222. for (; start != last; start+= delta)
  223. {
  224. for (i = 0; i < sslen; i++)
  225. {
  226. if (str[start + i] != sstr[i])
  227. break;
  228. }
  229. if (i == sslen)
  230. return (start + 1);
  231. }
  232. return 0;
  233. }
  234. /* Remove leading blanks from a string, padding at end. The src and dest
  235. should not overlap. */
  236. void
  237. adjustl (CHARTYPE *dest, gfc_charlen_type len, const CHARTYPE *src)
  238. {
  239. gfc_charlen_type i;
  240. i = 0;
  241. while (i < len && src[i] == ' ')
  242. i++;
  243. if (i < len)
  244. memcpy (dest, &src[i], (len - i) * sizeof (CHARTYPE));
  245. if (i > 0)
  246. MEMSET (&dest[len - i], ' ', i);
  247. }
  248. /* Remove trailing blanks from a string. */
  249. void
  250. adjustr (CHARTYPE *dest, gfc_charlen_type len, const CHARTYPE *src)
  251. {
  252. gfc_charlen_type i;
  253. i = len;
  254. while (i > 0 && src[i - 1] == ' ')
  255. i--;
  256. if (i < len)
  257. MEMSET (dest, ' ', len - i);
  258. memcpy (&dest[len - i], src, i * sizeof (CHARTYPE));
  259. }
  260. /* Scan a string for any one of the characters in a set of characters. */
  261. gfc_charlen_type
  262. string_scan (gfc_charlen_type slen, const CHARTYPE *str,
  263. gfc_charlen_type setlen, const CHARTYPE *set, GFC_LOGICAL_4 back)
  264. {
  265. gfc_charlen_type i, j;
  266. if (slen == 0 || setlen == 0)
  267. return 0;
  268. if (back)
  269. {
  270. for (i = slen; i != 0; i--)
  271. {
  272. for (j = 0; j < setlen; j++)
  273. {
  274. if (str[i - 1] == set[j])
  275. return i;
  276. }
  277. }
  278. }
  279. else
  280. {
  281. for (i = 0; i < slen; i++)
  282. {
  283. for (j = 0; j < setlen; j++)
  284. {
  285. if (str[i] == set[j])
  286. return (i + 1);
  287. }
  288. }
  289. }
  290. return 0;
  291. }
  292. /* Verify that a set of characters contains all the characters in a
  293. string by identifying the position of the first character in a
  294. characters that does not appear in a given set of characters. */
  295. gfc_charlen_type
  296. string_verify (gfc_charlen_type slen, const CHARTYPE *str,
  297. gfc_charlen_type setlen, const CHARTYPE *set,
  298. GFC_LOGICAL_4 back)
  299. {
  300. gfc_charlen_type start, last, delta, i;
  301. if (slen == 0)
  302. return 0;
  303. if (back)
  304. {
  305. last = -1;
  306. start = slen - 1;
  307. delta = -1;
  308. }
  309. else
  310. {
  311. last = slen;
  312. start = 0;
  313. delta = 1;
  314. }
  315. for (; start != last; start += delta)
  316. {
  317. for (i = 0; i < setlen; i++)
  318. {
  319. if (str[start] == set[i])
  320. break;
  321. }
  322. if (i == setlen)
  323. return (start + 1);
  324. }
  325. return 0;
  326. }
  327. /* MIN and MAX intrinsics for strings. The front-end makes sure that
  328. nargs is at least 2. */
  329. void
  330. string_minmax (gfc_charlen_type *rlen, CHARTYPE **dest, int op, int nargs, ...)
  331. {
  332. va_list ap;
  333. int i;
  334. CHARTYPE *next, *res;
  335. gfc_charlen_type nextlen, reslen;
  336. va_start (ap, nargs);
  337. reslen = va_arg (ap, gfc_charlen_type);
  338. res = va_arg (ap, CHARTYPE *);
  339. *rlen = reslen;
  340. if (res == NULL)
  341. runtime_error ("First argument of '%s' intrinsic should be present",
  342. op > 0 ? "MAX" : "MIN");
  343. for (i = 1; i < nargs; i++)
  344. {
  345. nextlen = va_arg (ap, gfc_charlen_type);
  346. next = va_arg (ap, CHARTYPE *);
  347. if (next == NULL)
  348. {
  349. if (i == 1)
  350. runtime_error ("Second argument of '%s' intrinsic should be "
  351. "present", op > 0 ? "MAX" : "MIN");
  352. else
  353. continue;
  354. }
  355. if (nextlen > *rlen)
  356. *rlen = nextlen;
  357. if (op * compare_string (reslen, res, nextlen, next) < 0)
  358. {
  359. reslen = nextlen;
  360. res = next;
  361. }
  362. }
  363. va_end (ap);
  364. if (*rlen == 0)
  365. *dest = &zero_length_string;
  366. else
  367. {
  368. CHARTYPE *tmp = xmallocarray (*rlen, sizeof (CHARTYPE));
  369. memcpy (tmp, res, reslen * sizeof (CHARTYPE));
  370. MEMSET (&tmp[reslen], ' ', *rlen - reslen);
  371. *dest = tmp;
  372. }
  373. }