~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Source/NSScanner.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
   Boston, MA 02111 USA.
25
25
 
26
26
   <title>NSScanner class reference</title>
27
 
   $Date: 2008-06-08 11:38:33 +0100 (Sun, 08 Jun 2008) $ $Revision: 26606 $
 
27
   $Date: 2010-02-25 11:49:31 -0700 (Thu, 25 Feb 2010) $ $Revision: 29753 $
28
28
*/
29
29
 
30
 
/* We need to define _GNU_SOURCE on systems (SuSE) to get LONG_LONG_MAX.  */
31
 
#ifndef _GNU_SOURCE
32
 
#define _GNU_SOURCE
33
 
#endif
34
 
 
35
 
#include "config.h"
36
 
#include "GNUstepBase/Unicode.h"
37
 
#include "Foundation/NSScanner.h"
38
 
#include "Foundation/NSException.h"
39
 
#include "Foundation/NSObjCRuntime.h"
40
 
#include "Foundation/NSUserDefaults.h"
41
 
 
42
 
#include <float.h>
43
 
#include <limits.h>
 
30
#import "common.h"
 
31
 
 
32
#if     defined(HAVE_FLOAT_H)
 
33
#include        <float.h>
 
34
#endif
 
35
 
 
36
#if     !defined(LLONG_MAX)
 
37
#  if   defined(__LONG_LONG_MAX__)
 
38
#    define LLONG_MAX __LONG_LONG_MAX__
 
39
#    define LLONG_MIN   (-LLONG_MAX-1)
 
40
#    define ULLONG_MAX  (LLONG_MAX * 2ULL + 1)
 
41
#  else
 
42
#    error Neither LLONG_MAX nor __LONG_LONG_MAX__ found
 
43
#  endif
 
44
#endif
 
45
 
44
46
#include <math.h>
45
47
#include <ctype.h>    /* FIXME: May go away once I figure out Unicode */
46
 
#include "GSPrivate.h"
47
 
 
48
 
/* BSD and Solaris have this */
49
 
#if defined(HANDLE_LLONG_MAX) && !defined(HANDLE_LONG_LONG_MAX)
50
 
#define LONG_LONG_MAX LLONG_MAX
51
 
#define LONG_LONG_MIN LLONG_MIN
52
 
#define ULONG_LONG_MAX ULLONG_MAX
53
 
#endif
 
48
 
 
49
#define EXPOSE_NSScanner_IVARS  1
 
50
#import "GNUstepBase/Unicode.h"
 
51
#import "Foundation/NSScanner.h"
 
52
#import "Foundation/NSException.h"
 
53
#import "Foundation/NSUserDefaults.h"
 
54
#import "GNUstepBase/NSObject+GNUstepBase.h"
 
55
 
 
56
#import "GSPrivate.h"
54
57
 
55
58
 
56
59
@class  GSCString;
87
90
/*
88
91
 * Hack for direct access to internals of an concrete string object.
89
92
 */
90
 
typedef struct {
91
 
  @defs(GSString)
92
 
} *ivars;
 
93
typedef GSString        *ivars;
93
94
#define myLength()      (((ivars)_string)->_count)
94
95
#define myUnicode(I)    (((ivars)_string)->_contents.u[I])
95
96
#define myChar(I)       myGetC((((ivars)_string)->_contents.c[I]))
201
202
      aString = @"";
202
203
    }
203
204
 
204
 
  c = GSObjCClass(aString);
 
205
  c = object_getClass(aString);
205
206
  if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES)
206
207
    {
207
208
      _isUnicode = YES;
240
241
    }
241
242
  else
242
243
    {
243
 
      RELEASE(self);
 
244
      DESTROY(self);
244
245
      NSLog(@"Scanner initialised with something not a string");
245
246
      return nil;
246
247
    }
339
340
  if (value)
340
341
    {
341
342
      if (overflow
342
 
        || (num > (negative ? (unsigned int)INT_MIN : (unsigned int)INT_MAX)))
 
343
        || (num > (negative ? (NSUInteger)INT_MIN : (NSUInteger)INT_MAX)))
343
344
        *value = negative ? INT_MIN: INT_MAX;
344
345
      else if (negative)
345
346
        *value = -num;
373
374
 * Scan an unsigned int of the given radix into value.
374
375
 * Internal version used by scanRadixUnsignedInt: and scanHexInt: .
375
376
 */
376
 
- (BOOL) scanUnsignedInt_: (unsigned int *)value
377
 
                    radix: (unsigned int)radix
 
377
- (BOOL) scanUnsignedInt_: (unsigned int*)value
 
378
                    radix: (NSUInteger)radix
378
379
                gotDigits: (BOOL)gotDigits
379
380
{
380
381
  unsigned int  num = 0;
462
463
 * <br/>
463
464
 * Scans past any excess digits
464
465
 */
465
 
- (BOOL) scanRadixUnsignedInt: (unsigned int *)value
 
466
- (BOOL) scanRadixUnsignedInt: (unsigned int*)value
466
467
{
467
468
  unsigned int  radix;
468
469
  BOOL          gotDigits = NO;
512
513
 * <br/>
513
514
 * Scans past any excess digits
514
515
 */
515
 
- (BOOL) scanHexInt: (unsigned int *)value
 
516
- (BOOL) scanHexInt: (unsigned int*)value
516
517
{
517
518
  unsigned int saveScanLocation = _scanLocation;
518
519
 
557
558
 * <br/>
558
559
 * Returns YES if anything is scanned, NO otherwise.
559
560
 * <br/>
560
 
 * On overflow, LONG_LONG_MAX or LONG_LONG_MIN is put into
 
561
 * On overflow, LLONG_MAX or LLONG_MIN is put into
561
562
 * <em>longLongValue</em>
562
563
 * <br/>
563
564
 * Scans past any excess digits
564
565
 */
565
566
- (BOOL) scanLongLong: (long long *)value
566
567
{
567
 
#if defined(LONG_LONG_MAX)
 
568
#if defined(LLONG_MAX)
568
569
  unsigned long long            num = 0;
569
 
  const unsigned long long      limit = ULONG_LONG_MAX / 10;
 
570
  const unsigned long long      limit = ULLONG_MAX / 10;
570
571
  BOOL                          negative = NO;
571
572
  BOOL                          overflow = NO;
572
573
  BOOL                          got_digits = NO;
621
622
    {
622
623
      if (negative)
623
624
        {
624
 
          if (overflow || (num > (unsigned long long)LONG_LONG_MIN))
625
 
            *value = LONG_LONG_MIN;
 
625
          if (overflow || (num > (unsigned long long)LLONG_MIN))
 
626
            *value = LLONG_MIN;
626
627
          else
627
628
            *value = -num;
628
629
        }
629
630
      else
630
631
        {
631
 
          if (overflow || (num > (unsigned long long)LONG_LONG_MAX))
632
 
            *value = LONG_LONG_MAX;
 
632
          if (overflow || (num > (unsigned long long)LLONG_MAX))
 
633
            *value = LLONG_MAX;
633
634
          else
634
635
            *value = num;
635
636
        }
636
637
    }
637
638
  return YES;
638
 
#else /* defined(LONG_LONG_MAX) */
 
639
#else /* defined(LLONG_MAX) */
639
640
  /*
640
641
   * Provide compile-time warning and run-time exception.
641
642
   */
643
644
  [NSException raise: NSGenericException
644
645
               format: @"Can't use long long variables."];
645
646
  return NO;
646
 
#endif /* defined(LONG_LONG_MAX) */
 
647
#endif /* defined(LLONG_MAX) */
647
648
}
648
649
 
649
650
/**
934
935
  NSRange       range;
935
936
  unsigned int  saveScanLocation = _scanLocation;
936
937
 
937
 
  skipToNextField();
 
938
  if (skipToNextField() == NO)
 
939
    {
 
940
      return NO;
 
941
    }
938
942
  range.location = _scanLocation;
939
943
  range.length = [string length];
940
944
  if (range.location + range.length > myLength())
988
992
  NSRange       found;
989
993
  unsigned int  saveScanLocation = _scanLocation;
990
994
 
991
 
  skipToNextField();
 
995
  if (skipToNextField() == NO)
 
996
    {
 
997
      return NO;
 
998
    }
992
999
  range.location = _scanLocation;
993
1000
  range.length = myLength() - _scanLocation;
994
1001
  found = [_string rangeOfString: string
1020
1027
 * scanning the string.  This is the position at which the next scan
1021
1028
 * operation will begin.
1022
1029
 */
1023
 
- (unsigned) scanLocation
 
1030
- (NSUInteger) scanLocation
1024
1031
{
1025
1032
  return _scanLocation;
1026
1033
}
1031
1038
 * Raises an NSRangeException if index is beyond the end of the
1032
1039
 * scanned string.
1033
1040
 */
1034
 
- (void) setScanLocation: (unsigned int)anIndex
 
1041
- (void) setScanLocation: (NSUInteger)anIndex
1035
1042
{
1036
1043
  if (_scanLocation <= myLength())
1037
1044
    _scanLocation = anIndex;
1219
1226
  if (result)
1220
1227
    {
1221
1228
      if (overflow
1222
 
        || (num > (negative ? (unsigned int)INT_MIN : (unsigned int)INT_MAX)))
 
1229
        || (num > (negative ? (NSUInteger)INT_MIN : (NSUInteger)INT_MAX)))
1223
1230
        *result = negative ? INT_MIN: INT_MAX;
1224
1231
      else if (negative)
1225
1232
        *result = -num;
1230
1237
}
1231
1238
 
1232
1239
/**
1233
 
 * Scan in a double value in the standard locale ('.' as decimal point).<br />
 
1240
 * Scan in a double value in the standard locale ('.' as decimal poNSInteger).<br />
1234
1241
 * Return YES on success, NO on failure.<br />
1235
1242
 * The value pointed to by result is unmodified on failure.<br />
1236
1243
 * No value is returned in result if it is a null pointer.
1247
1254
  unsigned      pos = 0;
1248
1255
 
1249
1256
  /* Skip whitespace */
1250
 
  while (pos < length && isspace((int)buf[pos]))
 
1257
  while (pos < length && isspace((NSInteger)buf[pos]))
1251
1258
    {
1252
1259
      pos++;
1253
1260
    }