home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Classes / RCString / RCStringCharacter.m < prev    next >
Encoding:
Text File  |  1993-01-19  |  1.7 KB  |  91 lines

  1. #import <RCString.h>
  2. /*
  3.     Copyright (C) 1992. Bruce Ediger.
  4.  
  5.       This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU Library General Public License.
  7. */
  8.  
  9. //
  10. // implementation of "character substitution and retrieval" methods
  11. //
  12.  
  13. @implementation RCString (Characters)
  14.  
  15. - (int)retrieveCharacterAt:(int)index
  16. {
  17.     int iRetval = 0;
  18.  
  19.     if (index >= 0 && p && p->s && p->l > index)
  20.         iRetval = p->s[index];
  21.  
  22.     return iRetval;
  23. }
  24.  
  25. - substituteCharacter:(int)aChar at:(int)index
  26. {
  27.     if (index >= 0 && p && p->s && p->l - 1 > index) {
  28.         if (p->n > 1)
  29.             [self copyReference];
  30.         p->s[index] = aChar;
  31.     }
  32.     return self;
  33. }
  34.  
  35. - insertCharacter:(int)aChar at:(int)index
  36. {
  37.     if (index >= 0 && p && p->s && p->l - 1 > index) {
  38.         char *bpNewString;
  39.         if (p->n > 1)
  40.             [self copyReference];
  41.         bpNewString = malloc(p->l + 1);
  42.         if (bpNewString) {
  43.             bcopy(p->s, bpNewString, index);
  44.             bpNewString[index] = aChar;
  45.             bcopy(&p->s[index], &bpNewString[index + 1], p->l - index);
  46.             free(p->s);
  47.             p->s = bpNewString;
  48.             ++p->l;
  49.         }
  50.     }
  51.     return self;
  52. }
  53.  
  54. #ifndef INDEX
  55. #define INDEX(a, b) index((a), (b))
  56. #define RINDEX(a, b) rindex((a), (b))
  57. #else
  58. #define INDEX(a, b) strchr((a), (b))
  59. #define RINDEX(a, b) strrchr((a), (b))
  60. #endif
  61.  
  62. - (int)indexOfCharacter:(int)aChar
  63. {
  64.     char *bpTheChar;
  65.     int   irRetval = -1;
  66.  
  67.     if (p && p->s) {
  68.         bpTheChar = INDEX(p->s, aChar);
  69.         if (bpTheChar)
  70.             irRetval = (unsigned int)bpTheChar - (unsigned int)p->s;
  71.     }
  72.  
  73.     return irRetval;
  74. }
  75.  
  76. - (int)lastIndexOfCharacter:(int)aChar
  77. {
  78.     char *bpTheChar;
  79.     int   irRetval = -1;
  80.  
  81.     if (p && p->s) {
  82.         bpTheChar = RINDEX(p->s, aChar);
  83.         if (bpTheChar)
  84.             irRetval = (unsigned int)bpTheChar - (unsigned int)p->s;
  85.     }
  86.  
  87.     return irRetval;
  88. }
  89.  
  90. @end
  91.