~ubuntu-branches/ubuntu/gutsy/gnustep-base/gutsy

« back to all changes in this revision

Viewing changes to Source/NSBitmapCharSet.m

  • Committer: Bazaar Package Importer
  • Author(s): Hubert Chan
  • Date: 2006-12-12 13:31:22 UTC
  • mfrom: (2.1.9 feisty)
  • Revision ID: james.westby@ubuntu.com-20061212133122-34qt7qitn5c8psw8
Tags: 1.13.0-7
Revert conflict: with tendra; rename pl binary to plio, as per Section
10.1 of Policy. (really closes: #402558)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** NSBitmapCharSet - Concrete character set holder
2
 
   Copyright (C) 1993,1994 Free Software Foundation, Inc.
3
 
 
4
 
   Written by:  Adam Fedor <fedor@boulder.colorado.edu>
5
 
   Date: Apr 1995
6
 
 
7
 
   This file is part of the GNUstep Base Library.
8
 
 
9
 
   This library is free software; you can redistribute it and/or
10
 
   modify it under the terms of the GNU Library General Public
11
 
   License as published by the Free Software Foundation; either
12
 
   version 2 of the License, or (at your option) any later version.
13
 
 
14
 
   This library is distributed in the hope that it will be useful,
15
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
   Library General Public License for more details.
18
 
 
19
 
   You should have received a copy of the GNU Library General Public
20
 
   License along with this library; if not, write to the Free
21
 
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
22
 
 
23
 
   <title>NSBitmapCharSet class reference</title>
24
 
   $Date: 2005/02/22 11:22:43 $ $Revision: 1.16 $
25
 
*/
26
 
 
27
 
#include "config.h"
28
 
#include "Foundation/NSBitmapCharSet.h"
29
 
#include "Foundation/NSException.h"
30
 
#include "Foundation/NSCoder.h"
31
 
 
32
 
@implementation NSBitmapCharSet
33
 
 
34
 
- (id) init
35
 
{
36
 
  return [self initWithBitmap: NULL];
37
 
}
38
 
 
39
 
/** Designated initializer */
40
 
- (id) initWithBitmap: (NSData*)bitmap
41
 
{
42
 
  [super init];
43
 
 
44
 
  if ([bitmap length] != BITMAP_SIZE)
45
 
    {
46
 
      NSLog(@"attempt to initialize character set with invalid bitmap");
47
 
      [self dealloc];
48
 
      return nil;
49
 
    }
50
 
 
51
 
  [bitmap getBytes: _data length: BITMAP_SIZE];
52
 
 
53
 
  return self;
54
 
}
55
 
 
56
 
- (NSData*) bitmapRepresentation
57
 
{
58
 
  return [NSData dataWithBytes: _data length: BITMAP_SIZE];
59
 
}
60
 
 
61
 
- (BOOL) characterIsMember: (unichar)aCharacter
62
 
{
63
 
  return ISSET(_data[aCharacter/8], aCharacter % 8);
64
 
}
65
 
 
66
 
- (void) encodeWithCoder: (NSCoder*)aCoder
67
 
{
68
 
  [aCoder encodeObject: [self bitmapRepresentation]];
69
 
}
70
 
 
71
 
- (id) initWithCoder: (NSCoder*)aCoder
72
 
{
73
 
  NSData        *rep;
74
 
 
75
 
  rep = [aCoder decodeObject];
76
 
  self = [self initWithBitmap: rep];
77
 
  return self;
78
 
}
79
 
 
80
 
@end
81
 
 
82
 
@implementation NSMutableBitmapCharSet
83
 
 
84
 
- (id) init
85
 
{
86
 
  return [self initWithBitmap: NULL];
87
 
}
88
 
 
89
 
/* Designated initializer */
90
 
- (id) initWithBitmap: (NSData*)bitmap
91
 
{
92
 
  [super init];
93
 
  if (bitmap)
94
 
    [bitmap getBytes: _data length: BITMAP_SIZE];
95
 
  return self;
96
 
}
97
 
 
98
 
- (void) encodeWithCoder: (NSCoder*)aCoder
99
 
{
100
 
  [aCoder encodeObject: [self bitmapRepresentation]];
101
 
}
102
 
 
103
 
- (id) initWithCoder: (NSCoder*)aCoder
104
 
{
105
 
  NSMutableData *rep;
106
 
 
107
 
  rep = [aCoder decodeObject];
108
 
  self = [self initWithBitmap: rep];
109
 
  return self;
110
 
}
111
 
 
112
 
/* Need to implement the next two methods just like NSBitmapCharSet */
113
 
- (NSData*) bitmapRepresentation
114
 
{
115
 
  return [NSData dataWithBytes: _data length: BITMAP_SIZE];
116
 
}
117
 
 
118
 
- (BOOL) characterIsMember: (unichar)aCharacter
119
 
{
120
 
  return ISSET(_data[aCharacter/8], aCharacter % 8);
121
 
}
122
 
 
123
 
- (void) addCharactersInRange: (NSRange)aRange
124
 
{
125
 
  unsigned i;
126
 
 
127
 
  if (NSMaxRange(aRange) > UNICODE_SIZE)
128
 
    {
129
 
      [NSException raise:NSInvalidArgumentException
130
 
          format:@"Specified range exceeds character set"];
131
 
      /* NOT REACHED */
132
 
    }
133
 
 
134
 
  for (i = aRange.location; i < NSMaxRange(aRange); i++)
135
 
    {
136
 
      SETBIT(_data[i/8], i % 8);
137
 
    }
138
 
}
139
 
 
140
 
- (void) addCharactersInString: (NSString*)aString
141
 
{
142
 
  unsigned   length;
143
 
 
144
 
  if (!aString)
145
 
    {
146
 
      [NSException raise:NSInvalidArgumentException
147
 
          format:@"Adding characters from nil string"];
148
 
      /* NOT REACHED */
149
 
    }
150
 
 
151
 
  length = [aString length];
152
 
  if (length > 0)
153
 
    {
154
 
      unsigned  i;
155
 
      unichar   (*get)(id, SEL, unsigned);
156
 
 
157
 
      get = (unichar (*)(id, SEL, unsigned))
158
 
        [aString methodForSelector: @selector(characterAtIndex:)];
159
 
      for (i = 0; i < length; i++)
160
 
        {
161
 
          unichar       letter;
162
 
 
163
 
          letter = (*get)(aString, @selector(characterAtIndex:), i);
164
 
          SETBIT(_data[letter/8], letter % 8);
165
 
        }
166
 
    }
167
 
}
168
 
 
169
 
- (void) formUnionWithCharacterSet: (NSCharacterSet*)otherSet
170
 
{
171
 
  unsigned      i;
172
 
  const char    *other_bytes;
173
 
 
174
 
  other_bytes = [[otherSet bitmapRepresentation] bytes];
175
 
  for (i = 0; i < BITMAP_SIZE; i++)
176
 
    {
177
 
      _data[i] = (_data[i] | other_bytes[i]);
178
 
    }
179
 
}
180
 
 
181
 
- (void) formIntersectionWithCharacterSet: (NSCharacterSet *)otherSet
182
 
{
183
 
  unsigned      i;
184
 
  const char    *other_bytes;
185
 
 
186
 
  other_bytes = [[otherSet bitmapRepresentation] bytes];
187
 
  for (i = 0; i < BITMAP_SIZE; i++)
188
 
    {
189
 
      _data[i] = (_data[i] & other_bytes[i]);
190
 
    }
191
 
}
192
 
 
193
 
- (void) removeCharactersInRange: (NSRange)aRange
194
 
{
195
 
  unsigned      i;
196
 
 
197
 
  if (NSMaxRange(aRange) > UNICODE_SIZE)
198
 
    {
199
 
      [NSException raise:NSInvalidArgumentException
200
 
          format:@"Specified range exceeds character set"];
201
 
      /* NOT REACHED */
202
 
    }
203
 
 
204
 
  for (i = aRange.location; i < NSMaxRange(aRange); i++)
205
 
    {
206
 
      CLRBIT(_data[i/8], i % 8);
207
 
    }
208
 
}
209
 
 
210
 
- (void) removeCharactersInString: (NSString*)aString
211
 
{
212
 
  unsigned      length;
213
 
 
214
 
  if (!aString)
215
 
    {
216
 
      [NSException raise:NSInvalidArgumentException
217
 
          format:@"Removing characters from nil string"];
218
 
      /* NOT REACHED */
219
 
    }
220
 
 
221
 
  length = [aString length];
222
 
  if (length > 0)
223
 
    {
224
 
      unsigned  i;
225
 
      unichar   (*get)(id, SEL, unsigned);
226
 
 
227
 
      get = (unichar (*)(id, SEL, unsigned))
228
 
        [aString methodForSelector: @selector(characterAtIndex:)];
229
 
 
230
 
      for (i = 0; i < length; i++)
231
 
        {
232
 
          unichar       letter;
233
 
 
234
 
          letter = (*get)(aString, @selector(characterAtIndex:), i);
235
 
          CLRBIT(_data[letter/8], letter % 8);
236
 
        }
237
 
    }
238
 
}
239
 
 
240
 
- (void) invert
241
 
{
242
 
  unsigned      i;
243
 
 
244
 
  for (i = 0; i < BITMAP_SIZE; i++)
245
 
    {
246
 
      _data[i] = ~_data[i];
247
 
    }
248
 
}
249
 
 
250
 
@end