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.
 
 
 
 

644 line
22 KiB

  1. /*
  2. *********************************************************************************************************
  3. * uC/LIB
  4. * Custom Library Modules
  5. *
  6. * Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
  7. *
  8. * SPDX-License-Identifier: APACHE-2.0
  9. *
  10. * This software is subject to an open source license and is distributed by
  11. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  12. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  13. *
  14. *********************************************************************************************************
  15. */
  16. /*
  17. *********************************************************************************************************
  18. *
  19. * ASCII CHARACTER OPERATIONS
  20. *
  21. * Filename : lib_ascii.c
  22. * Version : V1.39.01
  23. *********************************************************************************************************
  24. * Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
  25. *
  26. * (a) ALL standard library functions are implemented in the custom library modules :
  27. *
  28. * (1) \<Custom Library Directory>\lib_*.*
  29. *
  30. * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
  31. *
  32. * where
  33. * <Custom Library Directory> directory path for custom library software
  34. * <cpu> directory name for specific processor (CPU)
  35. * <compiler> directory name for specific compiler
  36. *
  37. * (b) Product-specific library functions are implemented in individual products.
  38. *
  39. *
  40. * (2) (a) ECMA-6 '7-Bit coded Character Set' (6th edition), which corresponds to the
  41. * 3rd edition of ISO 646, specifies several versions of a 7-bit character set :
  42. *
  43. * (1) THE GENERAL VERSION, which allows characters at 0x23 and 0x24 to be given a
  44. * set alternate form and allows the characters 0x40, 0x5B, 0x5D, 0x60, 0x7B &
  45. * 0x7D to be assigned a "unique graphic character" or to be declared as unused.
  46. * All other characters are explicitly specified.
  47. *
  48. * (2) THE INTERNATIONAL REFERENCE VERSION, which explicitly specifies all characters
  49. * in the 7-bit character set.
  50. *
  51. * (3) NATIONAL & APPLICATION-ORIENTED VERSIONS, which may be derived from the
  52. * standard in specified ways.
  53. *
  54. * (b) The character set represented in this file reproduces the Internation Reference
  55. * Version. This is identical to the 7-bit character set which occupies Unicode
  56. * characters 0x0000 through 0x007F. The character names are taken from v5.0 of the
  57. * Unicode specification, with certain abbreviations so that the resulting #define
  58. * names will not violate ANSI C naming restriction :
  59. *
  60. * (1) For the Latin capital & lowercase letters, the name components 'LETTER_CAPITAL'
  61. * & 'LETTER_SMALL' are replaced by 'UPPER' & 'LOWER', respectively.
  62. *********************************************************************************************************
  63. */
  64. /*
  65. *********************************************************************************************************
  66. * INCLUDE FILES
  67. *********************************************************************************************************
  68. */
  69. #define MICRIUM_SOURCE
  70. #define LIB_ASCII_MODULE
  71. #include <lib_ascii.h>
  72. /*
  73. *********************************************************************************************************
  74. * LOCAL DEFINES
  75. *********************************************************************************************************
  76. */
  77. /*
  78. *********************************************************************************************************
  79. * LOCAL CONSTANTS
  80. *********************************************************************************************************
  81. */
  82. /*
  83. *********************************************************************************************************
  84. * LOCAL DATA TYPES
  85. *********************************************************************************************************
  86. */
  87. /*
  88. *********************************************************************************************************
  89. * LOCAL TABLES
  90. *********************************************************************************************************
  91. */
  92. /*
  93. *********************************************************************************************************
  94. * LOCAL GLOBAL VARIABLES
  95. *********************************************************************************************************
  96. */
  97. /*
  98. *********************************************************************************************************
  99. * LOCAL FUNCTION PROTOTYPES
  100. *********************************************************************************************************
  101. */
  102. /*
  103. *********************************************************************************************************
  104. * LOCAL CONFIGURATION ERRORS
  105. *********************************************************************************************************
  106. */
  107. /*
  108. *********************************************************************************************************
  109. * ASCII_IsAlpha()
  110. *
  111. * Description : Determine whether a character is an alphabetic character.
  112. *
  113. * Argument(s) : c Character to examine.
  114. *
  115. * Return(s) : DEF_YES, if character is an alphabetic character.
  116. *
  117. * DEF_NO, if character is NOT an alphabetic character.
  118. *
  119. * Caller(s) : Application.
  120. *
  121. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.2.(2) states that "isalpha() returns true only for the
  122. * characters for which isupper() or islower() is true".
  123. *********************************************************************************************************
  124. */
  125. CPU_BOOLEAN ASCII_IsAlpha (CPU_CHAR c)
  126. {
  127. CPU_BOOLEAN alpha;
  128. alpha = ASCII_IS_ALPHA(c);
  129. return (alpha);
  130. }
  131. /*
  132. *********************************************************************************************************
  133. * ASCII_IsAlphaNum()
  134. *
  135. * Description : Determine whether a character is an alphanumeric character.
  136. *
  137. * Argument(s) : c Character to examine.
  138. *
  139. * Return(s) : DEF_YES, if character is an alphanumeric character.
  140. *
  141. * DEF_NO, if character is NOT an alphanumeric character.
  142. *
  143. * Caller(s) : Application.
  144. *
  145. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.1.(2) states that "isalnum() ... tests for any character
  146. * for which isalpha() or isdigit() is true".
  147. *********************************************************************************************************
  148. */
  149. CPU_BOOLEAN ASCII_IsAlphaNum (CPU_CHAR c)
  150. {
  151. CPU_BOOLEAN alpha_num;
  152. alpha_num = ASCII_IS_ALPHA_NUM(c);
  153. return (alpha_num);
  154. }
  155. /*
  156. *********************************************************************************************************
  157. * ASCII_IsLower()
  158. *
  159. * Description : Determine whether a character is a lowercase alphabetic character.
  160. *
  161. * Argument(s) : c Character to examine.
  162. *
  163. * Return(s) : DEF_YES, if character is a lowercase alphabetic character.
  164. *
  165. * DEF_NO, if character is NOT a lowercase alphabetic character.
  166. *
  167. * Caller(s) : Application.
  168. *
  169. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.7.(2) states that "islower() returns true only for
  170. * the lowercase letters".
  171. *********************************************************************************************************
  172. */
  173. CPU_BOOLEAN ASCII_IsLower (CPU_CHAR c)
  174. {
  175. CPU_BOOLEAN lower;
  176. lower = ASCII_IS_LOWER(c);
  177. return (lower);
  178. }
  179. /*
  180. *********************************************************************************************************
  181. * ASCII_IsUpper()
  182. *
  183. * Description : Determine whether a character is an uppercase alphabetic character.
  184. *
  185. * Argument(s) : c Character to examine.
  186. *
  187. * Return(s) : DEF_YES, if character is an uppercase alphabetic character.
  188. *
  189. * DEF_NO, if character is NOT an uppercase alphabetic character.
  190. *
  191. * Caller(s) : Application.
  192. *
  193. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.11.(2) states that "isupper() returns true only for
  194. * the uppercase letters".
  195. *********************************************************************************************************
  196. */
  197. CPU_BOOLEAN ASCII_IsUpper (CPU_CHAR c)
  198. {
  199. CPU_BOOLEAN upper;
  200. upper = ASCII_IS_UPPER(c);
  201. return (upper);
  202. }
  203. /*
  204. *********************************************************************************************************
  205. * ASCII_IsDig()
  206. *
  207. * Description : Determine whether a character is a decimal-digit character.
  208. *
  209. * Argument(s) : c Character to examine.
  210. *
  211. * Return(s) : DEF_YES, if character is a decimal-digit character.
  212. *
  213. * DEF_NO, if character is NOT a decimal-digit character.
  214. *
  215. * Caller(s) : Application.
  216. *
  217. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.5.(2) states that "isdigit() ... tests for any
  218. * decimal-digit character".
  219. *********************************************************************************************************
  220. */
  221. CPU_BOOLEAN ASCII_IsDig (CPU_CHAR c)
  222. {
  223. CPU_BOOLEAN dig;
  224. dig = ASCII_IS_DIG(c);
  225. return (dig);
  226. }
  227. /*
  228. *********************************************************************************************************
  229. * ASCII_IsDigOct()
  230. *
  231. * Description : Determine whether a character is an octal-digit character.
  232. *
  233. * Argument(s) : c Character to examine.
  234. *
  235. * Return(s) : DEF_YES, if character is an octal-digit character.
  236. *
  237. * DEF_NO, if character is NOT an octal-digit character.
  238. *
  239. * Caller(s) : Application.
  240. *
  241. * Note(s) : none.
  242. *********************************************************************************************************
  243. */
  244. CPU_BOOLEAN ASCII_IsDigOct (CPU_CHAR c)
  245. {
  246. CPU_BOOLEAN dig_oct;
  247. dig_oct = ASCII_IS_DIG_OCT(c);
  248. return (dig_oct);
  249. }
  250. /*
  251. *********************************************************************************************************
  252. * ASCII_IsDigHex()
  253. *
  254. * Description : Determine whether a character is a hexadecimal-digit character.
  255. *
  256. * Argument(s) : c Character to examine.
  257. *
  258. * Return(s) : DEF_YES, if character is a hexadecimal-digit character.
  259. *
  260. * DEF_NO, if character is NOT a hexadecimal-digit character.
  261. *
  262. * Caller(s) : Application.
  263. *
  264. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.12.(2) states that "isxdigit() ... tests for any
  265. * hexadecimal-digit character".
  266. *********************************************************************************************************
  267. */
  268. CPU_BOOLEAN ASCII_IsDigHex (CPU_CHAR c)
  269. {
  270. CPU_BOOLEAN dig_hex;
  271. dig_hex = ASCII_IS_DIG_HEX(c);
  272. return (dig_hex);
  273. }
  274. /*
  275. *********************************************************************************************************
  276. * ASCII_IsBlank()
  277. *
  278. * Description : Determine whether a character is a standard blank character.
  279. *
  280. * Argument(s) : c Character to examine.
  281. *
  282. * Return(s) : DEF_YES, if character is a standard blank character.
  283. *
  284. * DEF_NO, if character is NOT a standard blank character.
  285. *
  286. * Caller(s) : Application.
  287. *
  288. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.1.3.(2) states that "isblank() returns true only for
  289. * the standard blank characters".
  290. *
  291. * (b) ISO/IEC 9899:TC2, Section 7.4.1.3.(2) defines "the standard blank characters" as
  292. * the "space (' '), and horizontal tab ('\t')".
  293. *********************************************************************************************************
  294. */
  295. CPU_BOOLEAN ASCII_IsBlank (CPU_CHAR c)
  296. {
  297. CPU_BOOLEAN blank;
  298. blank = ASCII_IS_BLANK(c);
  299. return (blank);
  300. }
  301. /*
  302. *********************************************************************************************************
  303. * ASCII_IsSpace()
  304. *
  305. * Description : Determine whether a character is a white-space character.
  306. *
  307. * Argument(s) : c Character to examine.
  308. *
  309. * Return(s) : DEF_YES, if character is a white-space character.
  310. *
  311. * DEF_NO, if character is NOT a white-space character.
  312. *
  313. * Caller(s) : Application.
  314. *
  315. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.1.10.(2) states that "isspace() returns true only
  316. * for the standard white-space characters".
  317. *
  318. * (b) ISO/IEC 9899:TC2, Section 7.4.1.10.(2) defines "the standard white-space characters"
  319. * as the "space (' '), form feed ('\f'), new-line ('\n'), carriage return ('\r'),
  320. * horizontal tab ('\t'), and vertical tab ('\v')".
  321. *********************************************************************************************************
  322. */
  323. CPU_BOOLEAN ASCII_IsSpace (CPU_CHAR c)
  324. {
  325. CPU_BOOLEAN space;
  326. space = ASCII_IS_SPACE(c);
  327. return (space);
  328. }
  329. /*
  330. *********************************************************************************************************
  331. * ASCII_IsPrint()
  332. *
  333. * Description : Determine whether a character is a printing character.
  334. *
  335. * Argument(s) : c Character to examine.
  336. *
  337. * Return(s) : DEF_YES, if character is a printing character.
  338. *
  339. * DEF_NO, if character is NOT a printing character.
  340. *
  341. * Caller(s) : Application.
  342. *
  343. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.1.8.(2) states that "isprint() ... tests for any
  344. * printing character including space (' ')".
  345. *
  346. * (b) ISO/IEC 9899:TC2, Section 7.4.(3), Note 169, states that in "the seven-bit US
  347. * ASCII character set, the printing characters are those whose values lie from
  348. * 0x20 (space) through 0x7E (tilde)".
  349. *********************************************************************************************************
  350. */
  351. CPU_BOOLEAN ASCII_IsPrint (CPU_CHAR c)
  352. {
  353. CPU_BOOLEAN print;
  354. print = ASCII_IS_PRINT(c);
  355. return (print);
  356. }
  357. /*
  358. *********************************************************************************************************
  359. * ASCII_IsGraph()
  360. *
  361. * Description : Determine whether a character is any printing character except a space character.
  362. *
  363. * Argument(s) : c Character to examine.
  364. *
  365. * Return(s) : DEF_YES, if character is a graphic character.
  366. *
  367. * DEF_NO, if character is NOT a graphic character.
  368. *
  369. * Caller(s) : Application.
  370. *
  371. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.1.6.(2) states that "isgraph() ... tests for any
  372. * printing character except space (' ')".
  373. *
  374. * (b) ISO/IEC 9899:TC2, Section 7.4.(3), Note 169, states that in "the seven-bit US
  375. * ASCII character set, the printing characters are those whose values lie from
  376. * 0x20 (space) through 0x7E (tilde)".
  377. *********************************************************************************************************
  378. */
  379. CPU_BOOLEAN ASCII_IsGraph (CPU_CHAR c)
  380. {
  381. CPU_BOOLEAN graph;
  382. graph = ASCII_IS_GRAPH(c);
  383. return (graph);
  384. }
  385. /*
  386. *********************************************************************************************************
  387. * ASCII_IsPunct()
  388. *
  389. * Description : Determine whether a character is a punctuation character.
  390. *
  391. * Argument(s) : c Character to examine.
  392. *
  393. * Return(s) : DEF_YES, if character is a punctuation character.
  394. *
  395. * DEF_NO, if character is NOT a punctuation character.
  396. *
  397. * Caller(s) : Application.
  398. *
  399. * Note(s) : (1) ISO/IEC 9899:TC2, Section 7.4.1.9.(2) states that "ispunct() returns true for every
  400. * printing character for which neither isspace() nor isalnum() is true".
  401. *********************************************************************************************************
  402. */
  403. CPU_BOOLEAN ASCII_IsPunct (CPU_CHAR c)
  404. {
  405. CPU_BOOLEAN punct;
  406. punct = ASCII_IS_PUNCT(c);
  407. return (punct);
  408. }
  409. /*
  410. *********************************************************************************************************
  411. * ASCII_IsCtrl()
  412. *
  413. * Description : Determine whether a character is a control character.
  414. *
  415. * Argument(s) : c Character to examine.
  416. *
  417. * Return(s) : DEF_YES, if character is a control character.
  418. *
  419. * DEF_NO, if character is NOT a control character.
  420. *
  421. * Caller(s) : Application.
  422. *
  423. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.1.4.(2) states that "iscntrl() ... tests for any
  424. * control character".
  425. *
  426. * (b) ISO/IEC 9899:TC2, Section 7.4.(3), Note 169, states that in "the seven-bit US
  427. * ASCII character set, ... the control characters are those whose values lie from
  428. * 0 (NUL) through 0x1F (US), and the character 0x7F (DEL)".
  429. *********************************************************************************************************
  430. */
  431. CPU_BOOLEAN ASCII_IsCtrl (CPU_CHAR c)
  432. {
  433. CPU_BOOLEAN ctrl;
  434. ctrl = ASCII_IS_CTRL(c);
  435. return (ctrl);
  436. }
  437. /*
  438. *********************************************************************************************************
  439. * ASCII_ToLower()
  440. *
  441. * Description : Convert uppercase alphabetic character to its corresponding lowercase alphabetic character.
  442. *
  443. * Argument(s) : c Character to convert.
  444. *
  445. * Return(s) : Lowercase equivalent of 'c', if character 'c' is an uppercase character (see Note #1b1).
  446. *
  447. * Character 'c', otherwise (see Note #1b2).
  448. *
  449. * Caller(s) : Application.
  450. *
  451. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.2.1.(2) states that "tolower() ... converts an
  452. * uppercase letter to a corresponding lowercase letter".
  453. *
  454. * (b) ISO/IEC 9899:TC2, Section 7.4.2.1.(3) states that :
  455. *
  456. * (1) (A) "if the argument is a character for which isupper() is true and there are
  457. * one or more corresponding characters ... for which islower() is true," ...
  458. * (B) "tolower() ... returns one of the corresponding characters;" ...
  459. *
  460. * (2) "otherwise, the argument is returned unchanged."
  461. *********************************************************************************************************
  462. */
  463. CPU_CHAR ASCII_ToLower (CPU_CHAR c)
  464. {
  465. CPU_CHAR lower;
  466. lower = ASCII_TO_LOWER(c);
  467. return (lower);
  468. }
  469. /*
  470. *********************************************************************************************************
  471. * ASCII_ToUpper()
  472. *
  473. * Description : Convert lowercase alphabetic character to its corresponding uppercase alphabetic character.
  474. *
  475. * Argument(s) : c Character to convert.
  476. *
  477. * Return(s) : Uppercase equivalent of 'c', if character 'c' is a lowercase character (see Note #1b1).
  478. *
  479. * Character 'c', otherwise (see Note #1b2).
  480. *
  481. * Caller(s) : Application.
  482. *
  483. * Note(s) : (1) (a) ISO/IEC 9899:TC2, Section 7.4.2.2.(2) states that "toupper() ... converts a
  484. * lowercase letter to a corresponding uppercase letter".
  485. *
  486. * (b) ISO/IEC 9899:TC2, Section 7.4.2.2.(3) states that :
  487. *
  488. * (1) (A) "if the argument is a character for which islower() is true and there are
  489. * one or more corresponding characters ... for which isupper() is true," ...
  490. * (B) "toupper() ... returns one of the corresponding characters;" ...
  491. *
  492. * (2) "otherwise, the argument is returned unchanged."
  493. *********************************************************************************************************
  494. */
  495. CPU_CHAR ASCII_ToUpper (CPU_CHAR c)
  496. {
  497. CPU_CHAR upper;
  498. upper = ASCII_TO_UPPER(c);
  499. return (upper);
  500. }
  501. /*
  502. *********************************************************************************************************
  503. * ASCII_Cmp()
  504. *
  505. * Description : Determine if two characters are identical (case-insensitive).
  506. *
  507. * Argument(s) : c1 First character.
  508. *
  509. * c2 Second character.
  510. *
  511. * Return(s) : DEF_YES, if the characters are identical.
  512. *
  513. * DEF_NO, if the characters are NOT identical.
  514. *
  515. * Caller(s) : Application.
  516. *
  517. * Note(s) : none.
  518. *********************************************************************************************************
  519. */
  520. CPU_BOOLEAN ASCII_Cmp (CPU_CHAR c1,
  521. CPU_CHAR c2)
  522. {
  523. CPU_CHAR c1_upper;
  524. CPU_CHAR c2_upper;
  525. CPU_BOOLEAN cmp;
  526. c1_upper = ASCII_ToUpper(c1);
  527. c2_upper = ASCII_ToUpper(c2);
  528. cmp = (c1_upper == c2_upper) ? (DEF_YES) : (DEF_NO);
  529. return (cmp);
  530. }