home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / NString 1.0 beta / Sources / NString_Inlines.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-20  |  10.3 KB  |  419 lines  |  [TEXT/KAHL]

  1.  
  2. #ifndef _NSTRING_INLINES_H_
  3. #define _NSTRING_INLINES_H_
  4.  
  5. #include <string.h>
  6.  
  7. #if NSTRING_DEBUG > 0
  8.     #include <stdio.h>
  9. #endif
  10.  
  11. //__________________________________ PRIVATE INLINES __________________________________
  12.  
  13. inline NString::strbody::strbody()
  14. {
  15.     refs = 1;
  16.     len = 0;
  17.     bufsize = 0;
  18.     str = NULL;
  19. }
  20.  
  21. //__________________________________ PUBLIC INLINES __________________________________
  22.  
  23. //                                                         *** Constructors & Destructors ***
  24.  
  25.  
  26. inline NString::NString (const NString& s)    
  27. {
  28.     s.sb->refs++;
  29.     sb = s.sb;
  30.  
  31. #if (NSTRING_DEBUG & 2) != 0
  32.     printf("Construction of NString \"%s\" from another NString.\n", sb->str);
  33. #endif
  34. }
  35.  
  36. //_________________________________________________________________________________
  37.  
  38. //                                                 *** Accessing the NString's information ***
  39.  
  40.  
  41. inline char *NString::string(char *s) const
  42. {
  43.     strcpy(s, sb->str);
  44.     return s;
  45. }
  46.  
  47. //_________________________________________________________________________________
  48.  
  49. inline unsigned long int NString::length (void) const
  50. {
  51.     return sb->len;
  52. }
  53.  
  54. //_________________________________________________________________________________
  55.  
  56. //                                                                 *** Comparisons ***
  57.  
  58.  
  59. inline int NString::operator== (const char *s) const
  60. {
  61.     return (strcmp(sb->str, s) == 0);
  62. }
  63.  
  64. //_________________________________________________________________________________
  65.  
  66. inline int NString::operator!= (const char *s) const
  67. {
  68.     return (strcmp(sb->str, s) != 0);
  69. }
  70.  
  71. //_________________________________________________________________________________
  72.  
  73. inline int NString::operator>= (const char *s) const
  74. {
  75.     return (strcmp(sb->str, s) >= 0);
  76. }
  77.  
  78. //_________________________________________________________________________________
  79.  
  80. inline int NString::operator<= (const char *s) const
  81. {
  82.     return (strcmp(sb->str, s) <= 0);
  83. }
  84.  
  85. //_________________________________________________________________________________
  86.  
  87. inline int NString::operator> (const char *s) const
  88. {
  89.     return (strcmp(sb->str, s) > 0);
  90. }
  91.  
  92. //_________________________________________________________________________________
  93.  
  94. inline int NString::operator< (const char *s) const
  95. {
  96.     return (strcmp(sb->str, s) < 0);
  97. }
  98.  
  99. //_________________________________________________________________________________
  100.  
  101. inline int NString::compare (const char *s) const
  102. {
  103.     return (strcmp(sb->str, s));
  104. }
  105.  
  106. //_________________________________________________________________________________
  107.  
  108. inline int NString::operator== (const NString& s) const
  109. {
  110.     return (strcmp(sb->str, s.sb->str) == 0);
  111. }
  112.  
  113. //_________________________________________________________________________________
  114.  
  115. inline int NString::operator!= (const NString& s) const
  116. {
  117.     return (strcmp(sb->str, s.sb->str) != 0);
  118. }
  119.  
  120. //_________________________________________________________________________________
  121.  
  122. inline int NString::operator>= (const NString& s) const
  123. {
  124.     return (strcmp(sb->str, s.sb->str) >= 0);
  125. }
  126.  
  127. //_________________________________________________________________________________
  128.  
  129. inline int NString::operator<= (const NString& s) const
  130. {
  131.     return (strcmp(sb->str, s.sb->str) <= 0);
  132. }
  133.  
  134. //_________________________________________________________________________________
  135.  
  136. inline int NString::operator> (const NString& s) const
  137. {
  138.     return (strcmp(sb->str, s.sb->str) > 0);
  139. }
  140.  
  141. //_________________________________________________________________________________
  142.  
  143. inline int NString::operator< (const NString& s) const
  144. {
  145.     return (strcmp(sb->str, s.sb->str) < 0);
  146. }
  147.  
  148. //_________________________________________________________________________________
  149.  
  150. inline int NString::compare (const NString& s) const
  151. {
  152.     return (strcmp(sb->str, s.sb->str));
  153. }
  154.  
  155. //_________________________________________________________________________________
  156.  
  157. inline int NString::operator== (const char c) const
  158. {
  159.     return ((sb->len == 1) && (sb->str[0] == c));
  160. }
  161.  
  162. //_________________________________________________________________________________
  163.  
  164. inline int NString::operator!= (const char c) const
  165. {
  166.     return ((sb->len != 1) || (sb->str[0] != c));
  167. }
  168.  
  169. //_________________________________________________________________________________
  170.  
  171. inline int NString::operator>= (const char c) const
  172. {
  173.     return ((sb->len > 0) && (sb->str[0] >= c));
  174. }
  175.  
  176. //_________________________________________________________________________________
  177.  
  178. inline int NString::operator<= (const char c) const
  179. {
  180.     return ((sb->len == 0) || (sb->str[0] < c) || ((sb->len == 1) && (sb->str[0] == c)));
  181. }
  182.  
  183. //_________________________________________________________________________________
  184.  
  185. inline int NString::operator> (const char c) const
  186. {
  187.     return ((sb->str[0] > c) || ((sb->len > 1) && (sb->str[0] == c)));
  188. }
  189.  
  190. //_________________________________________________________________________________
  191.  
  192. inline int NString::operator< (const char c) const
  193. {
  194.     return ((sb->len == 0) || (sb->str[0] < c));
  195. }
  196.  
  197. //_________________________________________________________________________________
  198.  
  199. inline int NString::compare (const char c) const
  200. {
  201.     if ((sb->len == 0) || (sb->str[0] < c))
  202.         return (-1);
  203.     else if ((sb->len == 1) && (sb->str[0] == c))
  204.         return (0);
  205.     else
  206.         return (1);
  207. }
  208.  
  209. //_________________________________________________________________________________
  210.  
  211. inline const NString& NString::min (const NString& other) const
  212. {
  213.     return (*this < other ? *this : other);
  214. }
  215.  
  216. //_________________________________________________________________________________
  217.  
  218. inline const NString& NString::max (const NString& other) const
  219. {
  220.     return (*this > other ? *this : other);
  221. }
  222.  
  223. //_________________________________________________________________________________
  224.  
  225. //                                                             *** Getting a substring ***
  226.  
  227. inline NString NString::from (unsigned long int from) const
  228. {
  229.     return (fromto(from, sb->len));
  230. }
  231.  
  232. //_________________________________________________________________________________
  233.  
  234. inline NString NString::to (unsigned long int to) const
  235. {
  236.     return (fromto(0, to));
  237. }
  238.  
  239. //_________________________________________________________________________________
  240.  
  241. //                                                         *** Getting context position ***
  242.  
  243. inline unsigned long int NString::leftpos(const NString& s) const
  244. {
  245.     return (leftpos(s.sb->str));
  246. }
  247.  
  248. //_________________________________________________________________________________
  249.  
  250. inline unsigned long int NString::rightpos(const NString& s) const
  251. {
  252.     return (rightpos(s.sb->str));
  253. }
  254.  
  255. //_________________________________________________________________________________
  256.  
  257. //                                                 *** Getting a context related substring ***
  258.  
  259. inline char NString::leftocc (const Alphabet& a) const
  260. {
  261.     unsigned long int pos = leftpos(a);
  262.     
  263.     return ((pos == 0) ? '\0' : sb->str[pos-1]);
  264. }
  265.  
  266. //_________________________________________________________________________________
  267.  
  268. inline char NString::rightocc (const Alphabet& a) const
  269. {
  270.     unsigned long int pos = rightpos(a);
  271.     
  272.     return ((pos == 0) ? '\0' : sb->str[pos-1]);
  273. }
  274.  
  275. //_________________________________________________________________________________
  276.  
  277. //                                                         *** Context-related tests ***
  278.  
  279. inline int NString::startswith (const char *context) const
  280. {
  281.     return (strncmp(sb->str, context, strlen(context)) ? 0 : 1);
  282. }
  283.  
  284. //_________________________________________________________________________________
  285.  
  286. inline int NString::startswith (const char context) const
  287. {
  288.     return ((context != '\0') && (sb->str[0] == context));
  289. }
  290.  
  291. //_________________________________________________________________________________
  292.  
  293. inline int NString::startswith (const NString& context) const
  294. {
  295.     return (strncmp(sb->str, context.sb->str, context.sb->len) ? 0 : 1);
  296. }
  297.  
  298. //_________________________________________________________________________________
  299.  
  300. inline int NString::startswith (const Alphabet& context) const
  301. {
  302.     return ((sb->len != 0) && (context.contains(sb->str[0])));
  303. }
  304.  
  305. //_________________________________________________________________________________
  306.  
  307. inline int NString::endswith (const char context) const
  308. {
  309.     return ((sb->len != 0) && (sb->str[sb->len-1] == context));
  310. }
  311.  
  312. //_________________________________________________________________________________
  313.  
  314. inline int NString::endswith (const NString& context) const
  315. {
  316.     return (endswith(context.sb->str));
  317. }
  318.  
  319. //_________________________________________________________________________________
  320.  
  321. inline int NString::endswith (const Alphabet& context) const
  322. {
  323.     return ((sb->len != 0) && (context.contains(sb->str[sb->len - 1])));
  324. }
  325.  
  326. //_________________________________________________________________________________
  327.  
  328. inline int NString::contains (const char *context) const
  329. {
  330.     return (strstr(sb->str, context) != NULL);
  331. }
  332.  
  333. //_________________________________________________________________________________
  334.  
  335. inline int NString::contains (const char context) const
  336. {
  337.     char *ctx = "x";
  338.     
  339.     ctx[0] = context;
  340.     return (strstr(sb->str, ctx) != NULL);
  341. }
  342.  
  343. //_________________________________________________________________________________
  344.  
  345. inline int NString::contains (const NString& context) const
  346. {
  347.     return (strstr(sb->str, context.sb->str) != NULL);
  348. }
  349.  
  350. //_________________________________________________________________________________
  351.  
  352. inline int NString::contains (const Alphabet& context) const
  353. {
  354.     Alphabet a(sb->str);
  355.     
  356.     return (context <= a);
  357. }
  358.  
  359. //_________________________________________________________________________________
  360.  
  361. inline int NString::disjointed (const Alphabet& alpha) const
  362. {
  363.     return ((alpha.card() == 0) || (leftpos(alpha) == 0));
  364. }
  365.  
  366. //_________________________________________________________________________________
  367.  
  368. //                                                                 *** Input / Output ***
  369.  
  370.  
  371. inline istream& get (istream& theStream, NString& theString, const char terminator)
  372. {
  373.     return get(theStream, theString, 0, terminator);
  374. }
  375.  
  376. //_________________________________________________________________________________
  377.  
  378. inline NString& NString::operator<< (const char *s)
  379. {
  380.     return (*this += s);
  381. }
  382.  
  383. //_________________________________________________________________________________
  384.  
  385. inline NString& NString::operator<< (const NString& s)
  386. {
  387.     return (*this += s);
  388. }
  389.  
  390. //_________________________________________________________________________________
  391.  
  392. inline NString& NString::operator<< (const char c)
  393. {
  394.     return (*this += c);
  395. }
  396.  
  397. //_________________________________________________________________________________
  398.  
  399. inline NString& NString::operator<< (NString& (*manip)(NString&))
  400. {
  401.     return manip(*this);
  402. }
  403.  
  404. //_________________________________________________________________________________
  405.  
  406. inline NString& endl (NString& s)
  407. {
  408.     return (s += '\n');
  409. }
  410.  
  411. //_________________________________________________________________________________
  412.  
  413. inline NString& clear (NString& s)
  414. {
  415.     return s.clear();
  416. }
  417.  
  418. #endif
  419.