Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

622 lines
22 KiB

  1. /* handy.h
  2. *
  3. * Copyright (c) 1991-2001, Larry Wall
  4. *
  5. * You may distribute under the terms of either the GNU General Public
  6. * License or the Artistic License, as specified in the README file.
  7. *
  8. */
  9. #if !defined(__STDC__)
  10. #ifdef NULL
  11. #undef NULL
  12. #endif
  13. #ifndef I286
  14. # define NULL 0
  15. #else
  16. # define NULL 0L
  17. #endif
  18. #endif
  19. #define Null(type) ((type)NULL)
  20. /*
  21. =for apidoc AmU||Nullch
  22. Null character pointer.
  23. =for apidoc AmU||Nullsv
  24. Null SV pointer.
  25. =cut
  26. */
  27. #define Nullch Null(char*)
  28. #define Nullfp Null(PerlIO*)
  29. #define Nullsv Null(SV*)
  30. #ifdef TRUE
  31. #undef TRUE
  32. #endif
  33. #ifdef FALSE
  34. #undef FALSE
  35. #endif
  36. #define TRUE (1)
  37. #define FALSE (0)
  38. /* XXX Configure ought to have a test for a boolean type, if I can
  39. just figure out all the headers such a test needs.
  40. Andy Dougherty August 1996
  41. */
  42. /* bool is built-in for g++-2.6.3 and later, which might be used
  43. for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
  44. be sure _G_config.h will be included before this file. _G_config.h
  45. also defines _G_HAVE_BOOL for both gcc and g++, but only g++
  46. actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
  47. g++ can be identified by __GNUG__.
  48. Andy Dougherty February 2000
  49. */
  50. #ifdef __GNUG__ /* GNU g++ has bool built-in */
  51. # ifndef HAS_BOOL
  52. # define HAS_BOOL 1
  53. # endif
  54. #endif
  55. /* The NeXT dynamic loader headers will not build with the bool macro
  56. So declare them now to clear confusion.
  57. */
  58. #if defined(NeXT) || defined(__NeXT__)
  59. # undef FALSE
  60. # undef TRUE
  61. typedef enum bool { FALSE = 0, TRUE = 1 } bool;
  62. # define ENUM_BOOL 1
  63. # ifndef HAS_BOOL
  64. # define HAS_BOOL 1
  65. # endif /* !HAS_BOOL */
  66. #endif /* NeXT || __NeXT__ */
  67. #ifndef HAS_BOOL
  68. # if defined(UTS) || defined(VMS)
  69. # define bool int
  70. # else
  71. # define bool char
  72. # endif
  73. # define HAS_BOOL 1
  74. #endif
  75. /* XXX A note on the perl source internal type system. The
  76. original intent was that I32 be *exactly* 32 bits.
  77. Currently, we only guarantee that I32 is *at least* 32 bits.
  78. Specifically, if int is 64 bits, then so is I32. (This is the case
  79. for the Cray.) This has the advantage of meshing nicely with
  80. standard library calls (where we pass an I32 and the library is
  81. expecting an int), but the disadvantage that an I32 is not 32 bits.
  82. Andy Dougherty August 1996
  83. There is no guarantee that there is *any* integral type with
  84. exactly 32 bits. It is perfectly legal for a system to have
  85. sizeof(short) == sizeof(int) == sizeof(long) == 8.
  86. Similarly, there is no guarantee that I16 and U16 have exactly 16
  87. bits.
  88. For dealing with issues that may arise from various 32/64-bit
  89. systems, we will ask Configure to check out
  90. SHORTSIZE == sizeof(short)
  91. INTSIZE == sizeof(int)
  92. LONGSIZE == sizeof(long)
  93. LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
  94. PTRSIZE == sizeof(void *)
  95. DOUBLESIZE == sizeof(double)
  96. LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
  97. */
  98. #ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
  99. # include <inttypes.h>
  100. #endif
  101. typedef I8TYPE I8;
  102. typedef U8TYPE U8;
  103. typedef I16TYPE I16;
  104. typedef U16TYPE U16;
  105. typedef I32TYPE I32;
  106. typedef U32TYPE U32;
  107. #ifdef PERL_CORE
  108. # ifdef HAS_QUAD
  109. typedef I64TYPE I64;
  110. typedef U64TYPE U64;
  111. # endif
  112. #endif /* PERL_CORE */
  113. #if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
  114. # ifndef UINT64_C /* usually from <inttypes.h> */
  115. # if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
  116. # define INT64_C(c) CAT2(c,LL)
  117. # define UINT64_C(c) CAT2(c,ULL)
  118. # else
  119. # if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
  120. # define INT64_C(c) CAT2(c,L)
  121. # define UINT64_C(c) CAT2(c,UL)
  122. # else
  123. # define INT64_C(c) ((I64TYPE)(c))
  124. # define UINT64_C(c) ((U64TYPE)(c))
  125. # endif
  126. # endif
  127. # endif
  128. #endif
  129. /* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
  130. I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
  131. #if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
  132. /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
  133. Please search CHAR_MAX in perl.h for further details. */
  134. #define U8_MAX UINT8_MAX
  135. #define U8_MIN UINT8_MIN
  136. #define I16_MAX INT16_MAX
  137. #define I16_MIN INT16_MIN
  138. #define U16_MAX UINT16_MAX
  139. #define U16_MIN UINT16_MIN
  140. #define I32_MAX INT32_MAX
  141. #define I32_MIN INT32_MIN
  142. #define U32_MAX UINT32_MAX
  143. #define U32_MIN UINT32_MIN
  144. #else
  145. /* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
  146. Please search CHAR_MAX in perl.h for further details. */
  147. #define U8_MAX PERL_UCHAR_MAX
  148. #define U8_MIN PERL_UCHAR_MIN
  149. #define I16_MAX PERL_SHORT_MAX
  150. #define I16_MIN PERL_SHORT_MIN
  151. #define U16_MAX PERL_USHORT_MAX
  152. #define U16_MIN PERL_USHORT_MIN
  153. #if LONGSIZE > 4
  154. # define I32_MAX PERL_INT_MAX
  155. # define I32_MIN PERL_INT_MIN
  156. # define U32_MAX PERL_UINT_MAX
  157. # define U32_MIN PERL_UINT_MIN
  158. #else
  159. # define I32_MAX PERL_LONG_MAX
  160. # define I32_MIN PERL_LONG_MIN
  161. # define U32_MAX PERL_ULONG_MAX
  162. # define U32_MIN PERL_ULONG_MIN
  163. #endif
  164. #endif
  165. #define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
  166. #define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
  167. #define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
  168. #define Ctl(ch) ((ch) & 037)
  169. /*
  170. =for apidoc Am|bool|strNE|char* s1|char* s2
  171. Test two strings to see if they are different. Returns true or
  172. false.
  173. =for apidoc Am|bool|strEQ|char* s1|char* s2
  174. Test two strings to see if they are equal. Returns true or false.
  175. =for apidoc Am|bool|strLT|char* s1|char* s2
  176. Test two strings to see if the first, C<s1>, is less than the second,
  177. C<s2>. Returns true or false.
  178. =for apidoc Am|bool|strLE|char* s1|char* s2
  179. Test two strings to see if the first, C<s1>, is less than or equal to the
  180. second, C<s2>. Returns true or false.
  181. =for apidoc Am|bool|strGT|char* s1|char* s2
  182. Test two strings to see if the first, C<s1>, is greater than the second,
  183. C<s2>. Returns true or false.
  184. =for apidoc Am|bool|strGE|char* s1|char* s2
  185. Test two strings to see if the first, C<s1>, is greater than or equal to
  186. the second, C<s2>. Returns true or false.
  187. =for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
  188. Test two strings to see if they are different. The C<len> parameter
  189. indicates the number of bytes to compare. Returns true or false. (A
  190. wrapper for C<strncmp>).
  191. =for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
  192. Test two strings to see if they are equal. The C<len> parameter indicates
  193. the number of bytes to compare. Returns true or false. (A wrapper for
  194. C<strncmp>).
  195. =cut
  196. */
  197. #define strNE(s1,s2) (strcmp(s1,s2))
  198. #define strEQ(s1,s2) (!strcmp(s1,s2))
  199. #define strLT(s1,s2) (strcmp(s1,s2) < 0)
  200. #define strLE(s1,s2) (strcmp(s1,s2) <= 0)
  201. #define strGT(s1,s2) (strcmp(s1,s2) > 0)
  202. #define strGE(s1,s2) (strcmp(s1,s2) >= 0)
  203. #define strnNE(s1,s2,l) (strncmp(s1,s2,l))
  204. #define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
  205. #ifdef HAS_MEMCMP
  206. # define memNE(s1,s2,l) (memcmp(s1,s2,l))
  207. # define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
  208. #else
  209. # define memNE(s1,s2,l) (bcmp(s1,s2,l))
  210. # define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
  211. #endif
  212. /*
  213. * Character classes.
  214. *
  215. * Unfortunately, the introduction of locales means that we
  216. * can't trust isupper(), etc. to tell the truth. And when
  217. * it comes to /\w+/ with tainting enabled, we *must* be able
  218. * to trust our character classes.
  219. *
  220. * Therefore, the default tests in the text of Perl will be
  221. * independent of locale. Any code that wants to depend on
  222. * the current locale will use the tests that begin with "lc".
  223. */
  224. #ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
  225. # ifndef CTYPE256
  226. # define CTYPE256
  227. # endif
  228. #endif
  229. /*
  230. =for apidoc Am|bool|isALNUM|char ch
  231. Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
  232. character (including underscore) or digit.
  233. =for apidoc Am|bool|isALPHA|char ch
  234. Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
  235. character.
  236. =for apidoc Am|bool|isSPACE|char ch
  237. Returns a boolean indicating whether the C C<char> is whitespace.
  238. =for apidoc Am|bool|isDIGIT|char ch
  239. Returns a boolean indicating whether the C C<char> is an ASCII
  240. digit.
  241. =for apidoc Am|bool|isUPPER|char ch
  242. Returns a boolean indicating whether the C C<char> is an uppercase
  243. character.
  244. =for apidoc Am|bool|isLOWER|char ch
  245. Returns a boolean indicating whether the C C<char> is a lowercase
  246. character.
  247. =for apidoc Am|char|toUPPER|char ch
  248. Converts the specified character to uppercase.
  249. =for apidoc Am|char|toLOWER|char ch
  250. Converts the specified character to lowercase.
  251. =cut
  252. */
  253. #define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
  254. #define isIDFIRST(c) (isALPHA(c) || (c) == '_')
  255. #define isALPHA(c) (isUPPER(c) || isLOWER(c))
  256. #define isSPACE(c) \
  257. ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
  258. #define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
  259. #define isBLANK(c) ((c) == ' ' || (c) == '\t')
  260. #define isDIGIT(c) ((c) >= '0' && (c) <= '9')
  261. #ifdef EBCDIC
  262. /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
  263. # define isUPPER(c) isupper(c)
  264. # define isLOWER(c) islower(c)
  265. # define isALNUMC(c) isalnum(c)
  266. # define isASCII(c) isascii(c)
  267. # define isCNTRL(c) iscntrl(c)
  268. # define isGRAPH(c) isgraph(c)
  269. # define isPRINT(c) isprint(c)
  270. # define isPUNCT(c) ispunct(c)
  271. # define isXDIGIT(c) isxdigit(c)
  272. # define toUPPER(c) toupper(c)
  273. # define toLOWER(c) tolower(c)
  274. #else
  275. # define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
  276. # define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
  277. # define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
  278. # define isASCII(c) ((c) <= 127)
  279. # define isCNTRL(c) ((c) < ' ')
  280. # define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
  281. # define isPRINT(c) (((c) > 32 && (c) < 127) || isSPACE(c))
  282. # define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
  283. # define isXDIGIT(c) (isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
  284. # define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
  285. # define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
  286. #endif
  287. #ifdef USE_NEXT_CTYPE
  288. # define isALNUM_LC(c) \
  289. (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
  290. # define isIDFIRST_LC(c) \
  291. (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
  292. # define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
  293. # define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
  294. # define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
  295. # define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
  296. # define isLOWER_LC(c) NXIsLower((unsigned int)(c))
  297. # define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
  298. # define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
  299. # define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
  300. # define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
  301. # define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
  302. # define toUPPER_LC(c) NXToUpper((unsigned int)(c))
  303. # define toLOWER_LC(c) NXToLower((unsigned int)(c))
  304. #else /* !USE_NEXT_CTYPE */
  305. # if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
  306. # define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
  307. # define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
  308. # define isALPHA_LC(c) isalpha((unsigned char)(c))
  309. # define isSPACE_LC(c) isspace((unsigned char)(c))
  310. # define isDIGIT_LC(c) isdigit((unsigned char)(c))
  311. # define isUPPER_LC(c) isupper((unsigned char)(c))
  312. # define isLOWER_LC(c) islower((unsigned char)(c))
  313. # define isALNUMC_LC(c) isalnum((unsigned char)(c))
  314. # define isCNTRL_LC(c) iscntrl((unsigned char)(c))
  315. # define isGRAPH_LC(c) isgraph((unsigned char)(c))
  316. # define isPRINT_LC(c) isprint((unsigned char)(c))
  317. # define isPUNCT_LC(c) ispunct((unsigned char)(c))
  318. # define toUPPER_LC(c) toupper((unsigned char)(c))
  319. # define toLOWER_LC(c) tolower((unsigned char)(c))
  320. # else
  321. # define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
  322. # define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
  323. # define isALPHA_LC(c) (isascii(c) && isalpha(c))
  324. # define isSPACE_LC(c) (isascii(c) && isspace(c))
  325. # define isDIGIT_LC(c) (isascii(c) && isdigit(c))
  326. # define isUPPER_LC(c) (isascii(c) && isupper(c))
  327. # define isLOWER_LC(c) (isascii(c) && islower(c))
  328. # define isALNUMC_LC(c) (isascii(c) && isalnum(c))
  329. # define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
  330. # define isGRAPH_LC(c) (isascii(c) && isgraph(c))
  331. # define isPRINT_LC(c) (isascii(c) && isprint(c))
  332. # define isPUNCT_LC(c) (isascii(c) && ispunct(c))
  333. # define toUPPER_LC(c) toupper(c)
  334. # define toLOWER_LC(c) tolower(c)
  335. # endif
  336. #endif /* USE_NEXT_CTYPE */
  337. #define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
  338. #define isBLANK_LC(c) isBLANK(c) /* could be wrong */
  339. #define isALNUM_uni(c) is_uni_alnum(c)
  340. #define isIDFIRST_uni(c) is_uni_idfirst(c)
  341. #define isALPHA_uni(c) is_uni_alpha(c)
  342. #define isSPACE_uni(c) is_uni_space(c)
  343. #define isDIGIT_uni(c) is_uni_digit(c)
  344. #define isUPPER_uni(c) is_uni_upper(c)
  345. #define isLOWER_uni(c) is_uni_lower(c)
  346. #define isALNUMC_uni(c) is_uni_alnumc(c)
  347. #define isASCII_uni(c) is_uni_ascii(c)
  348. #define isCNTRL_uni(c) is_uni_cntrl(c)
  349. #define isGRAPH_uni(c) is_uni_graph(c)
  350. #define isPRINT_uni(c) is_uni_print(c)
  351. #define isPUNCT_uni(c) is_uni_punct(c)
  352. #define isXDIGIT_uni(c) is_uni_xdigit(c)
  353. #define toUPPER_uni(c) to_uni_upper(c)
  354. #define toTITLE_uni(c) to_uni_title(c)
  355. #define toLOWER_uni(c) to_uni_lower(c)
  356. #define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
  357. #define isBLANK_uni(c) isBLANK(c) /* could be wrong */
  358. #define isALNUM_LC_uni(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
  359. #define isIDFIRST_LC_uni(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
  360. #define isALPHA_LC_uni(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
  361. #define isSPACE_LC_uni(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
  362. #define isDIGIT_LC_uni(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
  363. #define isUPPER_LC_uni(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
  364. #define isLOWER_LC_uni(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
  365. #define isALNUMC_LC_uni(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
  366. #define isCNTRL_LC_uni(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
  367. #define isGRAPH_LC_uni(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
  368. #define isPRINT_LC_uni(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
  369. #define isPUNCT_LC_uni(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
  370. #define toUPPER_LC_uni(c) (c < 256 ? toUPPER_LC(c) : to_uni_upper_lc(c))
  371. #define toTITLE_LC_uni(c) (c < 256 ? toUPPER_LC(c) : to_uni_title_lc(c))
  372. #define toLOWER_LC_uni(c) (c < 256 ? toLOWER_LC(c) : to_uni_lower_lc(c))
  373. #define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
  374. #define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
  375. #define isALNUM_utf8(p) is_utf8_alnum(p)
  376. #define isIDFIRST_utf8(p) is_utf8_idfirst(p)
  377. #define isALPHA_utf8(p) is_utf8_alpha(p)
  378. #define isSPACE_utf8(p) is_utf8_space(p)
  379. #define isDIGIT_utf8(p) is_utf8_digit(p)
  380. #define isUPPER_utf8(p) is_utf8_upper(p)
  381. #define isLOWER_utf8(p) is_utf8_lower(p)
  382. #define isALNUMC_utf8(p) is_utf8_alnumc(p)
  383. #define isASCII_utf8(p) is_utf8_ascii(p)
  384. #define isCNTRL_utf8(p) is_utf8_cntrl(p)
  385. #define isGRAPH_utf8(p) is_utf8_graph(p)
  386. #define isPRINT_utf8(p) is_utf8_print(p)
  387. #define isPUNCT_utf8(p) is_utf8_punct(p)
  388. #define isXDIGIT_utf8(p) is_utf8_xdigit(p)
  389. #define toUPPER_utf8(p) to_utf8_upper(p)
  390. #define toTITLE_utf8(p) to_utf8_title(p)
  391. #define toLOWER_utf8(p) to_utf8_lower(p)
  392. #define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
  393. #define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
  394. #define isALNUM_LC_utf8(p) isALNUM_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  395. #define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  396. #define isALPHA_LC_utf8(p) isALPHA_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  397. #define isSPACE_LC_utf8(p) isSPACE_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  398. #define isDIGIT_LC_utf8(p) isDIGIT_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  399. #define isUPPER_LC_utf8(p) isUPPER_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  400. #define isLOWER_LC_utf8(p) isLOWER_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  401. #define isALNUMC_LC_utf8(p) isALNUMC_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  402. #define isCNTRL_LC_utf8(p) isCNTRL_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  403. #define isGRAPH_LC_utf8(p) isGRAPH_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  404. #define isPRINT_LC_utf8(p) isPRINT_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  405. #define isPUNCT_LC_utf8(p) isPUNCT_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  406. #define toUPPER_LC_utf8(p) toUPPER_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  407. #define toTITLE_LC_utf8(p) toTITLE_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  408. #define toLOWER_LC_utf8(p) toLOWER_LC_uni(utf8_to_uv(p, UTF8_MAXLEN, 0, 0))
  409. #define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
  410. #define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
  411. #ifdef EBCDIC
  412. # define toCTRL(c) Perl_ebcdic_control(c)
  413. #else
  414. /* This conversion works both ways, strangely enough. */
  415. # define toCTRL(c) (toUPPER(c) ^ 64)
  416. #endif
  417. /* Line numbers are unsigned, 16 bits. */
  418. typedef U16 line_t;
  419. #ifdef lint
  420. #define NOLINE ((line_t)0)
  421. #else
  422. #define NOLINE ((line_t) 65535)
  423. #endif
  424. /*
  425. XXX LEAKTEST doesn't really work in perl5. There are direct calls to
  426. safemalloc() in the source, so LEAKTEST won't pick them up.
  427. (The main "offenders" are extensions.)
  428. Further, if you try LEAKTEST, you'll also end up calling
  429. Safefree, which might call safexfree() on some things that weren't
  430. malloced with safexmalloc. The correct "fix" to this, if anyone
  431. is interested, is to ensure that all calls go through the New and
  432. Renew macros.
  433. --Andy Dougherty August 1996
  434. */
  435. /*
  436. =for apidoc Am|SV*|NEWSV|int id|STRLEN len
  437. Creates a new SV. A non-zero C<len> parameter indicates the number of
  438. bytes of preallocated string space the SV should have. An extra byte for a
  439. tailing NUL is also reserved. (SvPOK is not set for the SV even if string
  440. space is allocated.) The reference count for the new SV is set to 1.
  441. C<id> is an integer id between 0 and 1299 (used to identify leaks).
  442. =for apidoc Am|void|New|int id|void* ptr|int nitems|type
  443. The XSUB-writer's interface to the C C<malloc> function.
  444. =for apidoc Am|void|Newc|int id|void* ptr|int nitems|type|cast
  445. The XSUB-writer's interface to the C C<malloc> function, with
  446. cast.
  447. =for apidoc Am|void|Newz|int id|void* ptr|int nitems|type
  448. The XSUB-writer's interface to the C C<malloc> function. The allocated
  449. memory is zeroed with C<memzero>.
  450. =for apidoc Am|void|Renew|void* ptr|int nitems|type
  451. The XSUB-writer's interface to the C C<realloc> function.
  452. =for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
  453. The XSUB-writer's interface to the C C<realloc> function, with
  454. cast.
  455. =for apidoc Am|void|Safefree|void* ptr
  456. The XSUB-writer's interface to the C C<free> function.
  457. =for apidoc Am|void|Move|void* src|void* dest|int nitems|type
  458. The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
  459. source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
  460. the type. Can do overlapping moves. See also C<Copy>.
  461. =for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
  462. The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
  463. source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
  464. the type. May fail on overlapping copies. See also C<Move>.
  465. =for apidoc Am|void|Zero|void* dest|int nitems|type
  466. The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
  467. destination, C<nitems> is the number of items, and C<type> is the type.
  468. =for apidoc Am|void|StructCopy|type src|type dest|type
  469. This is an architecture-independent macro to copy one structure to another.
  470. =cut
  471. */
  472. #ifndef lint
  473. #define NEWSV(x,len) newSV(len)
  474. #ifndef LEAKTEST
  475. #define New(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
  476. #define Newc(x,v,n,t,c) (v = (c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))
  477. #define Newz(x,v,n,t) (v = (t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))), \
  478. memzero((char*)(v), (n)*sizeof(t))
  479. #define Renew(v,n,t) \
  480. (v = (t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  481. #define Renewc(v,n,t,c) \
  482. (v = (c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  483. #define Safefree(d) safefree((Malloc_t)(d))
  484. #else /* LEAKTEST */
  485. #define New(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
  486. #define Newc(x,v,n,t,c) (v = (c*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t))))
  487. #define Newz(x,v,n,t) (v = (t*)safexmalloc((x),(MEM_SIZE)((n)*sizeof(t)))), \
  488. memzero((char*)(v), (n)*sizeof(t))
  489. #define Renew(v,n,t) \
  490. (v = (t*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  491. #define Renewc(v,n,t,c) \
  492. (v = (c*)safexrealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))
  493. #define Safefree(d) safexfree((Malloc_t)(d))
  494. #define MAXXCOUNT 1400
  495. #define MAXY_SIZE 80
  496. #define MAXYCOUNT 16 /* (MAXY_SIZE/4 + 1) */
  497. extern long xcount[MAXXCOUNT];
  498. extern long lastxcount[MAXXCOUNT];
  499. extern long xycount[MAXXCOUNT][MAXYCOUNT];
  500. extern long lastxycount[MAXXCOUNT][MAXYCOUNT];
  501. #endif /* LEAKTEST */
  502. #define Move(s,d,n,t) (void)memmove((char*)(d),(char*)(s), (n) * sizeof(t))
  503. #define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
  504. #define Zero(d,n,t) (void)memzero((char*)(d), (n) * sizeof(t))
  505. #else /* lint */
  506. #define New(x,v,n,s) (v = Null(s *))
  507. #define Newc(x,v,n,s,c) (v = Null(s *))
  508. #define Newz(x,v,n,s) (v = Null(s *))
  509. #define Renew(v,n,s) (v = Null(s *))
  510. #define Move(s,d,n,t)
  511. #define Copy(s,d,n,t)
  512. #define Zero(d,n,t)
  513. #define Safefree(d) (d) = (d)
  514. #endif /* lint */
  515. #ifdef USE_STRUCT_COPY
  516. #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
  517. #else
  518. #define StructCopy(s,d,t) Copy(s,d,1,t)
  519. #endif