string_intrinsics_inc.c 10 KB

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