CharacterArray.st
changeset 345 cf2301210c47
parent 343 1cf554ea3773
child 350 54d513b45f51
equal deleted inserted replaced
344:4b35f99afefb 345:cf2301210c47
    19 
    19 
    20 CharacterArray comment:'
    20 CharacterArray comment:'
    21 COPYRIGHT (c) 1994 by Claus Gittinger
    21 COPYRIGHT (c) 1994 by Claus Gittinger
    22 	      All Rights Reserved
    22 	      All Rights Reserved
    23 
    23 
    24 $Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.20 1995-05-10 02:17:51 claus Exp $
    24 $Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.21 1995-05-16 17:06:01 claus Exp $
    25 '!
    25 '!
    26 
    26 
    27 !CharacterArray class methodsFor:'documentation'!
    27 !CharacterArray class methodsFor:'documentation'!
    28 
    28 
    29 copyright
    29 copyright
    40 "
    40 "
    41 !
    41 !
    42 
    42 
    43 version
    43 version
    44 "
    44 "
    45 $Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.20 1995-05-10 02:17:51 claus Exp $
    45 $Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.21 1995-05-16 17:06:01 claus Exp $
    46 "
    46 "
    47 !
    47 !
    48 
    48 
    49 documentation
    49 documentation
    50 "
    50 "
   125 
   125 
   126 asUppercaseFirst
   126 asUppercaseFirst
   127     "return a copy of myself where the first character is
   127     "return a copy of myself where the first character is
   128      converted to uppercase."
   128      converted to uppercase."
   129 
   129 
   130     |newString|
   130     |newString sz|
   131 
   131 
   132     newString := self copy.
   132     sz := self size.
   133     newString size > 0 ifTrue:[
   133     newString := self copyFrom:1 to:sz.
       
   134     sz > 0 ifTrue:[
   134 	newString at:1 put:(newString at:1) asUppercase
   135 	newString at:1 put:(newString at:1) asUppercase
   135     ].
   136     ].
   136     ^ newString
   137     ^ newString
   137 
   138 
   138     "
   139     "
   144 
   145 
   145 asLowercaseFirst
   146 asLowercaseFirst
   146     "return a copy of myself where the first character is
   147     "return a copy of myself where the first character is
   147      converted to lowercase."
   148      converted to lowercase."
   148 
   149 
   149     |newString|
   150     |newString sz|
   150 
   151 
   151     newString := self copy.
   152     sz := self size.
   152     newString size > 0 ifTrue:[
   153     newString := self copyFrom:1 to:sz.
       
   154     sz > 0 ifTrue:[
   153 	newString at:1 put:(newString at:1) asLowercase
   155 	newString at:1 put:(newString at:1) asLowercase
   154     ].
   156     ].
   155     ^ newString
   157     ^ newString
   156 
   158 
   157     "
   159     "
   188     ^ ComposedText fromString:self
   190     ^ ComposedText fromString:self
   189 !
   191 !
   190 
   192 
   191 asNumber
   193 asNumber
   192     "read a number from the receiver.
   194     "read a number from the receiver.
   193      Notice, that errors may occur during the read, so you better
   195      Notice, that (in contrast to ST-80) errors may occur during the read, 
   194      setup some signal handler when using this method."
   196      so you better setup some signal handler when using this method.
       
   197      This may change if ANSI specifies it."
       
   198 
       
   199 "/ ST-80 behavior:
       
   200 "/  ^ Number readFromString:self onError:0
   195 
   201 
   196     ^ Number readFromString:self
   202     ^ Number readFromString:self
   197 
   203 
   198     "
   204     "
   199      '123'     asNumber
   205      '123'     asNumber
   200      '123.567' asNumber
   206      '123.567' asNumber
   201      '(5/6)'   asNumber
   207      '(5/6)'   asNumber
   202      'foo'     asNumber
   208      'foo'     asNumber
   203      Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] 
   209      Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] 
       
   210     "
       
   211 !
       
   212 
       
   213 asNumberFromFormatString:ignored
       
   214     "read a number from the receiver, ignoring any nonDigit characters.
       
   215      This is typically used to convert from strings which include
       
   216      dollar-signs or millenium digits. However, this method also ignores
       
   217      the decimal point (if any) and therefore should be used with care."
       
   218 
       
   219     |tempString|
       
   220 
       
   221     tempString := self collect:[:char | char isDigit].
       
   222     ^ Number readFromString:tempString onError:0
       
   223 
       
   224     "
       
   225      'USD 123' asNumberFromFormatString:'foo'
       
   226      'DM 123'  asNumberFromFormatString:'foo'
       
   227      '123'     asNumberFromFormatString:'foo'
       
   228      '123.567' asNumberFromFormatString:'foo'
       
   229      '(5/6)'   asNumberFromFormatString:'foo'
       
   230      'foo'     asNumberFromFormatString:'foo'
   204     "
   231     "
   205 !
   232 !
   206 
   233 
   207 asInteger
   234 asInteger
   208     "read an integer from the receiver.
   235     "read an integer from the receiver.
   379     "
   406     "
   380      'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:)
   407      'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:)
   381      'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) 
   408      'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) 
   382      'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) 
   409      'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) 
   383     "
   410     "
       
   411 !
       
   412 
       
   413 tokensBasedOn:aCharacter
       
   414     "this is an ST-80 alias for the ST/X method
       
   415 	asCollectionOfSubstringsSeparatedBy:"
       
   416 
       
   417     ^ self asCollectionOfSubstringsSeparatedBy:aCharacter
       
   418 
       
   419     "
       
   420      'hello:world:isnt:this nice' tokensBasedOn:$:
       
   421      'foo,bar,baz' tokensBasedOn:$,
       
   422      '/etc/passwd' asFilename readStream nextLine tokensBasedOn:$:
       
   423     "
   384 ! !
   424 ! !
   385 
   425 
   386 !CharacterArray methodsFor:'special string converting'!
   426 !CharacterArray methodsFor:'special string converting'!
       
   427 
       
   428 chopTo:maxLen
       
   429     "if the receivers size is less or equal to maxLen, return it.
       
   430      Otherwise, return a copy of the receiver, where some characters 
       
   431      in the middle have been removed for a total string length
       
   432      of maxLen."
       
   433 
       
   434     |sz n1 n2|
       
   435 
       
   436     (sz := self size) > maxLen ifTrue:[
       
   437 	n1 := n2 := maxLen // 2.
       
   438 	maxLen odd ifTrue:[
       
   439 	    n2 := n1 + 1
       
   440 	].
       
   441 	^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1)
       
   442     ]
       
   443 
       
   444     "
       
   445      '12345678901234'   chopTo:15            
       
   446      '123456789012345'  chopTo:15         
       
   447      '1234567890123456' chopTo:15      
       
   448      'aShortString' chopTo:15 
       
   449      'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 
       
   450     "
       
   451 !
   387 
   452 
   388 contractTo:maxLen
   453 contractTo:maxLen
   389     "if the receivers size is less or equal to maxLen, return it.
   454     "if the receivers size is less or equal to maxLen, return it.
   390      Otherwise, return a copy of the receiver, where some characters 
   455      Otherwise, return a copy of the receiver, where some characters 
   391      in the middle have been replaced by '...' for a total string length
   456      in the middle have been replaced by '...' for a total string length
   401     ]
   466     ]
   402 
   467 
   403     "
   468     "
   404      '12345678901234' contractTo:15          
   469      '12345678901234' contractTo:15          
   405      '123456789012345' contractTo:15          
   470      '123456789012345' contractTo:15          
   406      '1234567890123456' contractTo:15         
   471      '1234567890123456' contractTo:15        
   407      'aShortString' contractTo:15 
   472      'aShortString' contractTo:15 
   408      'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 
   473      'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 
   409     "
   474     "
   410 !
   475 !
   411 
   476 
   794 
   859 
   795 > aString
   860 > aString
   796     "Compare the receiver with the argument and return true if the
   861     "Compare the receiver with the argument and return true if the
   797      receiver is greater than the argument. Otherwise return false.
   862      receiver is greater than the argument. Otherwise return false.
   798      In contrast to ST-80, case differences are NOT ignored, thus
   863      In contrast to ST-80, case differences are NOT ignored, thus
   799      'foo' > 'Foo' will return true. 
   864      'foo' > 'Foo' will return true; use #sameAs: to compare ignoring cases.. 
   800      Since this is incompatible to ST-80, this may change."
   865      Since this is incompatible to ST-80, this may change."
   801 
   866 
   802     |mySize    "{ Class: SmallInteger }"
   867     |mySize    "{ Class: SmallInteger }"
   803      otherSize "{ Class: SmallInteger }" 
   868      otherSize "{ Class: SmallInteger }" 
   804      n         "{ Class: SmallInteger }" 
   869      n         "{ Class: SmallInteger }" 
   820 = aString
   885 = aString
   821     "Compare the receiver with the argument and return true if the
   886     "Compare the receiver with the argument and return true if the
   822      receiver is equal to the argument. Otherwise return false.
   887      receiver is equal to the argument. Otherwise return false.
   823      This compare does NOT ignore case differences, 
   888      This compare does NOT ignore case differences, 
   824      therefore 'foo' = 'Foo' will return false.
   889      therefore 'foo' = 'Foo' will return false.
   825      Since this is incompatible to ST-80, this may change."
   890      Since this is incompatible to ST-80 (at least, V2.x) , this may change."
   826 
   891 
   827     |mySize    "{ Class: SmallInteger }"
   892     |mySize    "{ Class: SmallInteger }"
   828      otherSize |
   893      otherSize |
   829 
   894 
   830     aString species == self species ifFalse:[^ false].
   895     aString species == self species ifFalse:[^ false].
   842      'foo' = 'bar'  
   907      'foo' = 'bar'  
   843      'foo' = 'foo'   
   908      'foo' = 'foo'   
   844     "
   909     "
   845 !
   910 !
   846 
   911 
   847 trueCompare:aString
   912 compareWith:aString
   848     "Compare the receiver with the argument and return 1 if the receiver is
   913     "Compare the receiver with the argument and return 1 if the receiver is
   849      greater, 0 if equal and -1 if less than the argument. 
   914      greater, 0 if equal and -1 if less than the argument. 
   850      Case differences are NOT ignored, thus
   915      Case differences are NOT ignored, thus
   851      'foo' trueCompare: 'Foo' will return 1."
   916      'foo' compareWith: 'Foo' will return 1."
   852 
   917 
   853     |mySize    "{ Class: SmallInteger }"
   918     |mySize    "{ Class: SmallInteger }"
   854      otherSize "{ Class: SmallInteger }" 
   919      otherSize "{ Class: SmallInteger }" 
   855      n         "{ Class: SmallInteger }" 
   920      n         "{ Class: SmallInteger }" 
   856      c1 c2|
   921      c1 c2|
   892 
   957 
   893     "
   958     "
   894      'foo' sameAs: 'Foo'   
   959      'foo' sameAs: 'Foo'   
   895      'foo' sameAs: 'bar' 
   960      'foo' sameAs: 'bar' 
   896      'foo' sameAs: 'foo'   
   961      'foo' sameAs: 'foo'   
       
   962     "
       
   963 !
       
   964 
       
   965 sameCharacters:aString
       
   966     "count & return the number of characters which are the same
       
   967      (ignoring case) in the receiver and the argument, aString."
       
   968 
       
   969     |n "{ Class: SmallInteger }"
       
   970      otherSize c1 c2 cnt|
       
   971 
       
   972     n := self size.
       
   973     n := n min:(aString size).
       
   974 
       
   975     cnt := 0.
       
   976     1 to:n do:[:index |
       
   977 	c1 := self at:index.
       
   978 	c2 := aString at:index.
       
   979 	((c1 == c2)
       
   980 	or:[c1 asLowercase = c2 asLowercase]) ifTrue:[
       
   981 	    cnt := cnt + 1
       
   982 	]
       
   983     ].
       
   984     ^ cnt
       
   985 
       
   986     "
       
   987      'foobarbaz' sameCharacters: 'foo'   
       
   988      'foobarbaz' sameCharacters: 'Foo'   
       
   989      'foobarbaz' sameCharacters: 'baz'   
   897     "
   990     "
   898 ! !
   991 ! !
   899 
   992 
   900 !CharacterArray methodsFor:'character searching'!
   993 !CharacterArray methodsFor:'character searching'!
   901 
   994 
  1308 
  1401 
  1309 match:aString
  1402 match:aString
  1310     "return true if aString matches self, where self may contain meta-match 
  1403     "return true if aString matches self, where self may contain meta-match 
  1311      characters $* (to match any string) or $# (to match any character).
  1404      characters $* (to match any string) or $# (to match any character).
  1312      or [...] to match a set of characters.
  1405      or [...] to match a set of characters.
  1313      Lower/uppercase are considered different."
  1406      Lower/uppercase are considered different.
       
  1407      NOTICE: match-meta character interpretation is like in unix-matching, 
       
  1408 	     NOT the ST-80 meaning."
  1314 
  1409 
  1315     ^ self match:aString from:1 to:aString size ignoreCase:false
  1410     ^ self match:aString from:1 to:aString size ignoreCase:false
  1316 
  1411 
  1317     "
  1412     "
  1318      '*ute*' match:'computer' 
  1413      '*ute*' match:'computer' 
  1328 
  1423 
  1329 match:aString ignoreCase:ignoreCase
  1424 match:aString ignoreCase:ignoreCase
  1330     "return true if aString matches self, where self may contain meta-match 
  1425     "return true if aString matches self, where self may contain meta-match 
  1331      characters $* (to match any string) or $# (to match any character)
  1426      characters $* (to match any string) or $# (to match any character)
  1332      or [...] to match a set of characters.
  1427      or [...] to match a set of characters.
  1333      If ignoreCase is true, lower/uppercase are considered the same."
  1428      If ignoreCase is true, lower/uppercase are considered the same.
       
  1429      NOTICE: match-meta character interpretation is like in unix-matching, 
       
  1430 	     NOT the ST-80 meaning."
  1334 
  1431 
  1335     ^ self match:aString from:1 to:aString size ignoreCase:ignoreCase
  1432     ^ self match:aString from:1 to:aString size ignoreCase:ignoreCase
  1336 
  1433 
  1337     "
  1434     "
  1338      '*ute*' match:'COMPUTER' ignoreCase:true  
  1435      '*ute*' match:'COMPUTER' ignoreCase:true  
  1357 match:aString from:start to:stop ignoreCase:ignoreCase
  1454 match:aString from:start to:stop ignoreCase:ignoreCase
  1358     "return true if part of aString matches myself, 
  1455     "return true if part of aString matches myself, 
  1359      where self may contain meta-match 
  1456      where self may contain meta-match 
  1360      characters $* (to match any string) or $# (to match any character)
  1457      characters $* (to match any string) or $# (to match any character)
  1361      or [...] to match a set of characters.
  1458      or [...] to match a set of characters.
  1362      If ignoreCase is true, lower/uppercase are considered the same."
  1459      If ignoreCase is true, lower/uppercase are considered the same.
       
  1460      NOTICE: match-meta character interpretation is like in unix-matching, 
       
  1461 	     NOT the ST-80 meaning."
  1363 
  1462 
  1364     |matchScanArray|
  1463     |matchScanArray|
  1365 
  1464 
  1366     "
  1465     "
  1367      keep the matchScanArray from the most recent match -
  1466      keep the matchScanArray from the most recent match -