str.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // str.CPP
  21. //
  22. // History:
  23. // 05/20/97 JMI Started.
  24. //
  25. // 05/21/97 JMI Added a table to replace the LOWER() macro for speed as
  26. // per Jeff's suggestion.
  27. //
  28. // 05/21/97 BRH Fixed rspStrnicmp to recognize completion correctly.
  29. //
  30. // 06/29/97 MJR Added const's and switched to size_t for count to bring
  31. // into full compliance with ASNI equivalent.
  32. //
  33. //////////////////////////////////////////////////////////////////////////////
  34. //
  35. // Useful, generic string functions that are not ANSI standard.
  36. //
  37. //////////////////////////////////////////////////////////////////////////////
  38. //////////////////////////////////////////////////////////////////////////////
  39. // C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
  40. //////////////////////////////////////////////////////////////////////////////
  41. ///////////////////////////////////////////////////////////////////////////////
  42. // RSPiX Headers.
  43. // If PATHS_IN_INCLUDES macro is defined, we can utilize relative
  44. // paths to a header file. In this case we generally go off of our
  45. // RSPiX root directory. System.h MUST be included before this macro
  46. // is evaluated. System.h is the header that, based on the current
  47. // platform (or more so in this case on the compiler), defines
  48. // PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
  49. // instead.
  50. ///////////////////////////////////////////////////////////////////////////////
  51. #include "Blue.h"
  52. #ifdef PATHS_IN_INCLUDES
  53. #include "ORANGE/str/str.h"
  54. #else
  55. #include "str.h"
  56. #endif
  57. //////////////////////////////////////////////////////////////////////////////
  58. // Module specific macros.
  59. //////////////////////////////////////////////////////////////////////////////
  60. // Get the lowercase equivalent, if alpha.
  61. #define LOWER(c) ( ( (c) >= 'A' && (c) <= 'Z') ? (c) - 32 : (c) )
  62. //////////////////////////////////////////////////////////////////////////////
  63. // Module specific typedefs.
  64. //////////////////////////////////////////////////////////////////////////////
  65. //////////////////////////////////////////////////////////////////////////////
  66. // Exported (extern) variables.
  67. //////////////////////////////////////////////////////////////////////////////
  68. //////////////////////////////////////////////////////////////////////////////
  69. // Module specific (static) variables / Instantiate class statics.
  70. //////////////////////////////////////////////////////////////////////////////
  71. static short ms_asUpper2Lower[256] =
  72. {
  73. 0,
  74. 1,
  75. 2,
  76. 3,
  77. 4,
  78. 5,
  79. 6,
  80. 7,
  81. 8,
  82. 9,
  83. 10,
  84. 11,
  85. 12,
  86. 13,
  87. 14,
  88. 15,
  89. 16,
  90. 17,
  91. 18,
  92. 19,
  93. 20,
  94. 21,
  95. 22,
  96. 23,
  97. 24,
  98. 25,
  99. 26,
  100. 27,
  101. 28,
  102. 29,
  103. 30,
  104. 31,
  105. 32,
  106. 33,
  107. 34,
  108. 35,
  109. 36,
  110. 37,
  111. 38,
  112. 39,
  113. 40,
  114. 41,
  115. 42,
  116. 43,
  117. 44,
  118. 45,
  119. 46,
  120. 47,
  121. 48,
  122. 49,
  123. 50,
  124. 51,
  125. 52,
  126. 53,
  127. 54,
  128. 55,
  129. 56,
  130. 57,
  131. 58,
  132. 59,
  133. 60,
  134. 61,
  135. 62,
  136. 63,
  137. 64,
  138. 'a',
  139. 'b',
  140. 'c',
  141. 'd',
  142. 'e',
  143. 'f',
  144. 'g',
  145. 'h',
  146. 'i',
  147. 'j',
  148. 'k',
  149. 'l',
  150. 'm',
  151. 'n',
  152. 'o',
  153. 'p',
  154. 'q',
  155. 'r',
  156. 's',
  157. 't',
  158. 'u',
  159. 'v',
  160. 'w',
  161. 'x',
  162. 'y',
  163. 'z',
  164. 91,
  165. 92,
  166. 93,
  167. 94,
  168. 95,
  169. 96,
  170. 97,
  171. 98,
  172. 99,
  173. 100,
  174. 101,
  175. 102,
  176. 103,
  177. 104,
  178. 105,
  179. 106,
  180. 107,
  181. 108,
  182. 109,
  183. 110,
  184. 111,
  185. 112,
  186. 113,
  187. 114,
  188. 115,
  189. 116,
  190. 117,
  191. 118,
  192. 119,
  193. 120,
  194. 121,
  195. 122,
  196. 123,
  197. 124,
  198. 125,
  199. 126,
  200. 127,
  201. 128,
  202. 129,
  203. 130,
  204. 131,
  205. 132,
  206. 133,
  207. 134,
  208. 135,
  209. 136,
  210. 137,
  211. 138,
  212. 139,
  213. 140,
  214. 141,
  215. 142,
  216. 143,
  217. 144,
  218. 145,
  219. 146,
  220. 147,
  221. 148,
  222. 149,
  223. 150,
  224. 151,
  225. 152,
  226. 153,
  227. 154,
  228. 155,
  229. 156,
  230. 157,
  231. 158,
  232. 159,
  233. 160,
  234. 161,
  235. 162,
  236. 163,
  237. 164,
  238. 165,
  239. 166,
  240. 167,
  241. 168,
  242. 169,
  243. 170,
  244. 171,
  245. 172,
  246. 173,
  247. 174,
  248. 175,
  249. 176,
  250. 177,
  251. 178,
  252. 179,
  253. 180,
  254. 181,
  255. 182,
  256. 183,
  257. 184,
  258. 185,
  259. 186,
  260. 187,
  261. 188,
  262. 189,
  263. 190,
  264. 191,
  265. 192,
  266. 193,
  267. 194,
  268. 195,
  269. 196,
  270. 197,
  271. 198,
  272. 199,
  273. 200,
  274. 201,
  275. 202,
  276. 203,
  277. 204,
  278. 205,
  279. 206,
  280. 207,
  281. 208,
  282. 209,
  283. 210,
  284. 211,
  285. 212,
  286. 213,
  287. 214,
  288. 215,
  289. 216,
  290. 217,
  291. 218,
  292. 219,
  293. 220,
  294. 221,
  295. 222,
  296. 223,
  297. 224,
  298. 225,
  299. 226,
  300. 227,
  301. 228,
  302. 229,
  303. 230,
  304. 231,
  305. 232,
  306. 233,
  307. 234,
  308. 235,
  309. 236,
  310. 237,
  311. 238,
  312. 239,
  313. 240,
  314. 241,
  315. 242,
  316. 243,
  317. 244,
  318. 245,
  319. 246,
  320. 247,
  321. 248,
  322. 249,
  323. 250,
  324. 251,
  325. 252,
  326. 253,
  327. 254,
  328. 255
  329. };
  330. //////////////////////////////////////////////////////////////////////////////
  331. // Module specific (static) protos.
  332. //////////////////////////////////////////////////////////////////////////////
  333. //////////////////////////////////////////////////////////////////////////////
  334. // Functions.
  335. //////////////////////////////////////////////////////////////////////////////
  336. //////////////////////////////////////////////////////////////////////////////
  337. // Perform a lowercase comparison of strings.
  338. // If the strings are equal up to the end of the shorter string, that
  339. // string is lesser.
  340. // Excerpt from VC 5.0 Help on strnicmp():
  341. // "Two strings containing characters located between 'Z' and 'a' in the ASCII
  342. // table ('[', '\', ']', '^', '_', and '`') compare differently, depending on
  343. // their case. For example, the two strings "ABCDE" and "ABCD^" compare one
  344. // way if the comparison is lowercase ("abcde" > "abcd^") and the other way
  345. // ("ABCDE" < "ABCD^") if it is uppercase."
  346. //////////////////////////////////////////////////////////////////////////////
  347. extern short rspStricmp( // Returns 0 if equivalent.
  348. // Returns < 0 if pszStr1 less than pszStr2.
  349. // Returns > 0 if pszStr1 greater than pszStr2.
  350. const char* pszStr1, // In: First string to compare.
  351. const char* pszStr2) // In: Second string to compare.
  352. {
  353. short sRes = 0; // Assume equivalent.
  354. while (*pszStr1 != '\0' && *pszStr2 != '\0' && sRes == 0)
  355. {
  356. sRes = ms_asUpper2Lower[*pszStr1++] - ms_asUpper2Lower[*pszStr2++];
  357. }
  358. // If identical . . .
  359. if (sRes == 0)
  360. {
  361. // If first string ended prematurely . . .
  362. if (*pszStr1 == '\0' && *pszStr2 != '\0')
  363. {
  364. sRes = -1;
  365. }
  366. // Else, if second string ended prematurely . . .
  367. else if (*pszStr1 != '\0' && *pszStr2 == '\0')
  368. {
  369. sRes = 1;
  370. }
  371. }
  372. return sRes;
  373. }
  374. //////////////////////////////////////////////////////////////////////////////
  375. // Perform a lowercase comparison of strings up to n characters.
  376. // If the shorter string ends before n characters and the strings are
  377. // equal up to the end of the shorter string, that string is lesser.
  378. // Excerpt from VC 5.0 Help on strnicmp():
  379. // "Two strings containing characters located between 'Z' and 'a' in the ASCII
  380. // table ('[', '\', ']', '^', '_', and '`') compare differently, depending on
  381. // their case. For example, the two strings "ABCDE" and "ABCD^" compare one
  382. // way if the comparison is lowercase ("abcde" > "abcd^") and the other way
  383. // ("ABCDE" < "ABCD^") if it is uppercase."
  384. //////////////////////////////////////////////////////////////////////////////
  385. extern short rspStrnicmp( // Returns 0 if equivalent.
  386. // Returns < 0 if pszStr1 less than pszStr2.
  387. // Returns > 0 if pszStr1 greater than pszStr2.
  388. const char* pszStr1, // In: First string to compare.
  389. const char* pszStr2, // In: Second string to compare.
  390. size_t count) // In: Number of characters to compare.
  391. {
  392. ASSERT(count >= 0);
  393. short sRes = 0; // Assume equivalent.
  394. while (*pszStr1 != '\0' && *pszStr2 != '\0' && sRes == 0 && count--)
  395. {
  396. sRes = ms_asUpper2Lower[*pszStr1++] - ms_asUpper2Lower[*pszStr2++];
  397. }
  398. // If identical . . .
  399. if (sRes == 0 && count != 0)
  400. {
  401. // If first string ended prematurely . . .
  402. if (*pszStr1 == '\0' && *pszStr2 != '\0')
  403. {
  404. sRes = -1;
  405. }
  406. // Else, if second string ended prematurely . . .
  407. else if (*pszStr1 != '\0' && *pszStr2 == '\0')
  408. {
  409. sRes = 1;
  410. }
  411. }
  412. return sRes;
  413. }
  414. ///////////////////////////////////////////////////////////////////////////////
  415. // EOF
  416. ///////////////////////////////////////////////////////////////////////////////