Character.st
changeset 699 12f456343eea
parent 544 d78012b20769
child 700 b4ae5ce39bfc
equal deleted inserted replaced
698:04533375e12c 699:12f456343eea
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 Magnitude subclass:#Character
    13 Magnitude subclass:#Character
    14        instanceVariableNames:'asciivalue'
    14 	 instanceVariableNames:'asciivalue'
    15        classVariableNames:''
    15 	 classVariableNames:''
    16        poolDictionaries:''
    16 	 poolDictionaries:''
    17        category:'Magnitude-General'
    17 	 category:'Magnitude-General'
    18 !
    18 !
    19 
    19 
    20 !Character class methodsFor:'documentation'!
    20 !Character class methodsFor:'documentation'!
    21 
    21 
    22 copyright
    22 copyright
    31  other person.  No title to or ownership of the software is
    31  other person.  No title to or ownership of the software is
    32  hereby transferred.
    32  hereby transferred.
    33 "
    33 "
    34 !
    34 !
    35 
    35 
    36 version
       
    37     ^' $Header: /cvs/stx/stx/libbasic/Character.st,v 1.26 1995-11-14 19:01:21 cg Exp $'
       
    38 !
       
    39 
       
    40 documentation
    36 documentation
    41 "
    37 "
    42     Single byte Characters are unique; this means that for every asciiValue (0..255) there
    38     Single byte Characters are unique; this means that for every asciiValue (0..255) there
    43     is exactly one instance of Character, which is shared.
    39     is exactly one instance of Character, which is shared.
    44     Other characters (i.e. asciivalue > 255) are not shared.
    40     Other characters (i.e. asciivalue > 255) are not shared.
    68 
    64 
    69 basicNew
    65 basicNew
    70     "catch new - Characters cannot be created with new"
    66     "catch new - Characters cannot be created with new"
    71 
    67 
    72     ^ self error:'Characters cannot be created with new'
    68     ^ self error:'Characters cannot be created with new'
       
    69 !
       
    70 
       
    71 digitValue:anInteger
       
    72     "return a character that corresponds to anInteger.
       
    73      0-9 map to $0-$9, 10-35 map to $A-$Z"
       
    74 
       
    75     |val "{ Class: SmallInteger }" |
       
    76 
       
    77     val := anInteger.
       
    78     (val between:0 and:9) ifTrue:[
       
    79 	^ Character value:(val + ($0 asciiValue))
       
    80     ].
       
    81     (val between:10 and:35) ifTrue:[
       
    82 	^ Character value:(val + ($A asciiValue - 10))
       
    83     ].
       
    84     ^self error:'value not in range 0 to 35'
    73 !
    85 !
    74 
    86 
    75 value:anInteger
    87 value:anInteger
    76     "return a character with asciivalue anInteger"
    88     "return a character with asciivalue anInteger"
    77 
    89 
    95     "
   107     "
    96      a characters ascii-code must be 0..16rFFFF.
   108      a characters ascii-code must be 0..16rFFFF.
    97      (i.e. only single-byte and twoByte characters are allowed.)
   109      (i.e. only single-byte and twoByte characters are allowed.)
    98     "
   110     "
    99     self error:'invalid ascii code for character'
   111     self error:'invalid ascii code for character'
   100 !
   112 ! !
   101 
   113 
   102 digitValue:anInteger
   114 !Character class methodsFor:'constants'!
   103     "return a character that corresponds to anInteger.
   115 
   104      0-9 map to $0-$9, 10-35 map to $A-$Z"
   116 backspace
   105 
   117     "return the backspace character"
   106     |val "{ Class: SmallInteger }" |
   118 
   107 
   119     ^ Character value:8
   108     val := anInteger.
   120 !
   109     (val between:0 and:9) ifTrue:[
   121 
   110 	^ Character value:(val + ($0 asciiValue))
   122 bell
   111     ].
   123     "return the bell character"
   112     (val between:10 and:35) ifTrue:[
   124 
   113 	^ Character value:(val + ($A asciiValue - 10))
   125     ^ Character value:7
   114     ].
   126 !
   115     ^self error:'value not in range 0 to 35'
   127 
       
   128 cr
       
   129     "return the lineEnd character 
       
   130      - actually (in unix) this is a newline character"
       
   131 
       
   132     ^ Character value:10
       
   133 !
       
   134 
       
   135 del 
       
   136     "return the delete character"
       
   137 
       
   138     ^ Character value:16r7F 
       
   139 !
       
   140 
       
   141 doubleQuote
       
   142     "return the double-quote character"
       
   143 
       
   144     ^ Character value:34
       
   145 !
       
   146 
       
   147 esc
       
   148     "return the escape character"
       
   149 
       
   150     ^ Character value:27
       
   151 !
       
   152 
       
   153 excla
       
   154     "return the exclamation-mark character"
       
   155     ^ $!!
       
   156 !
       
   157 
       
   158 ff
       
   159     "return the form-feed character"
       
   160 
       
   161     ^ Character value:12
       
   162 !
       
   163 
       
   164 lf
       
   165     "return the newline/linefeed character"
       
   166 
       
   167     ^ Character value:10
       
   168 !
       
   169 
       
   170 maxValue 
       
   171     "return the maximum asciiValue a character can have"
       
   172 
       
   173     ^ 16rFFFF
       
   174 !
       
   175 
       
   176 newPage
       
   177     "return the form-feed character"
       
   178 
       
   179     ^ Character value:12
       
   180 !
       
   181 
       
   182 nl
       
   183     "return the newline character"
       
   184 
       
   185     ^ Character value:10
       
   186 !
       
   187 
       
   188 quote
       
   189     "return the single-quote character"
       
   190 
       
   191     ^ Character value:39
       
   192 !
       
   193 
       
   194 return 
       
   195     "return the return character.
       
   196      In ST/X, this is different from cr - for Unix reasons."
       
   197 
       
   198     ^ Character value:13 
       
   199 !
       
   200 
       
   201 space
       
   202     "return the blank character"
       
   203 
       
   204     ^ Character value:32
       
   205 !
       
   206 
       
   207 tab
       
   208     "return the tabulator character"
       
   209 
       
   210     ^ Character value:9
   116 ! !
   211 ! !
   117 
   212 
   118 !Character class methodsFor:'primitive input'!
   213 !Character class methodsFor:'primitive input'!
   119 
   214 
   120 fromUser
   215 fromUser
   139      - this class is known by the run-time-system (but not subclasses)"
   234      - this class is known by the run-time-system (but not subclasses)"
   140 
   235 
   141     ^ self == Character
   236     ^ self == Character
   142 ! !
   237 ! !
   143 
   238 
   144 !Character class methodsFor:'constants'!
       
   145 
       
   146 bell
       
   147     "return the bell character"
       
   148 
       
   149     ^ Character value:7
       
   150 !
       
   151 
       
   152 backspace
       
   153     "return the backspace character"
       
   154 
       
   155     ^ Character value:8
       
   156 !
       
   157 
       
   158 del 
       
   159     "return the delete character"
       
   160 
       
   161     ^ Character value:16r7F 
       
   162 !
       
   163 
       
   164 nl
       
   165     "return the newline character"
       
   166 
       
   167     ^ Character value:10
       
   168 !
       
   169 
       
   170 lf
       
   171     "return the newline/linefeed character"
       
   172 
       
   173     ^ Character value:10
       
   174 !
       
   175 
       
   176 cr
       
   177     "return the lineEnd character 
       
   178      - actually (in unix) this is a newline character"
       
   179 
       
   180     ^ Character value:10
       
   181 !
       
   182 
       
   183 return 
       
   184     "return the return character.
       
   185      In ST/X, this is different from cr - for Unix reasons."
       
   186 
       
   187     ^ Character value:13 
       
   188 !
       
   189 
       
   190 tab
       
   191     "return the tabulator character"
       
   192 
       
   193     ^ Character value:9
       
   194 !
       
   195 
       
   196 newPage
       
   197     "return the form-feed character"
       
   198 
       
   199     ^ Character value:12
       
   200 !
       
   201 
       
   202 ff
       
   203     "return the form-feed character"
       
   204 
       
   205     ^ Character value:12
       
   206 !
       
   207 
       
   208 space
       
   209     "return the blank character"
       
   210 
       
   211     ^ Character value:32
       
   212 !
       
   213 
       
   214 esc
       
   215     "return the escape character"
       
   216 
       
   217     ^ Character value:27
       
   218 !
       
   219 
       
   220 quote
       
   221     "return the single-quote character"
       
   222 
       
   223     ^ Character value:39
       
   224 !
       
   225 
       
   226 doubleQuote
       
   227     "return the double-quote character"
       
   228 
       
   229     ^ Character value:34
       
   230 !
       
   231 
       
   232 excla
       
   233     "return the exclamation-mark character"
       
   234     ^ $!!
       
   235 !
       
   236 
       
   237 maxValue 
       
   238     "return the maximum asciiValue a character can have"
       
   239 
       
   240     ^ 16rFFFF
       
   241 ! !
       
   242 
       
   243 !Character methodsFor:'copying'!
       
   244 
       
   245 copy
       
   246     "return a copy of myself
       
   247      reimplemented since characters are unique"
       
   248 
       
   249      ^ self
       
   250 !
       
   251 
       
   252 shallowCopy
       
   253     "return a shallow copy of myself
       
   254      reimplemented since characters are unique"
       
   255 
       
   256      ^ self
       
   257 !
       
   258 
       
   259 deepCopy
       
   260     "return a deep copy of myself
       
   261      reimplemented since characters are unique"
       
   262 
       
   263      ^ self
       
   264 !
       
   265 
       
   266 simpleDeepCopy
       
   267     "return a deep copy of myself
       
   268      reimplemented since characters are unique"
       
   269 
       
   270      ^ self
       
   271 !
       
   272 
       
   273 deepCopyUsing:aDictionary
       
   274     "return a deep copy of myself
       
   275      reimplemented since characters are unique"
       
   276 
       
   277      ^ self
       
   278 ! !
       
   279 
       
   280 !Character methodsFor:'private accessing'!
       
   281 
       
   282 setAsciiValue:anInteger
       
   283     "very private - set the ascii value. 
       
   284      - use this only for characters with codes > 16rFF.
       
   285      DANGER alert: funny things happen, if this is applied to
       
   286      one of the fixed-characters 0..255."
       
   287 
       
   288     asciivalue := anInteger
       
   289 ! !
       
   290         
       
   291 !Character methodsFor:'accessing'!
   239 !Character methodsFor:'accessing'!
   292 
   240 
   293 asciiValue
   241 asciiValue
   294     "return the asciivalue of myself.
   242     "return the asciivalue of myself.
   295      PP has removed this methhod with 4.1 and providing
   243      PP has removed this methhod with 4.1 and providing
   302     "catch instvar access - asciivalue may not be changed"
   250     "catch instvar access - asciivalue may not be changed"
   303 
   251 
   304     self error:'Characters may not be modified'
   252     self error:'Characters may not be modified'
   305 ! !
   253 ! !
   306 
   254 
   307 !Character methodsFor:'comparing'!
   255 !Character methodsFor:'arithmetic'!
   308 
       
   309 = aCharacter
       
   310     "return true, if the argument, aCharacter is the same character
       
   311      Redefined to take care of 16bit characters."
       
   312 
       
   313     self == aCharacter ifTrue:[^ true].
       
   314     aCharacter isCharacter ifFalse:[^ false].
       
   315     ^ (asciivalue = aCharacter asciiValue)
       
   316 !
       
   317 
       
   318 ~= aCharacter
       
   319     "return true, if the argument, aCharacter is not the same character
       
   320      Redefined to take care of 16bit characters."
       
   321 
       
   322     self == aCharacter ifTrue:[^ false].
       
   323     aCharacter isCharacter ifFalse:[^ true].
       
   324     ^ (asciivalue ~~ aCharacter asciiValue)
       
   325 !
       
   326 
       
   327 sameAs:aCharacter
       
   328     "return true, if the argument, aCharacter is the same character,
       
   329      ignoring case differences."
       
   330 
       
   331     self == aCharacter ifTrue:[^ true].
       
   332     ^ self asLowercase = aCharacter asLowercase
       
   333 !
       
   334 
       
   335 > aCharacter
       
   336     "return true, if the arguments asciiValue is less than mine"
       
   337 
       
   338     ^ (asciivalue > aCharacter asciiValue)
       
   339 !
       
   340 
       
   341 < aCharacter
       
   342     "return true, if the arguments asciiValue is greater than mine"
       
   343 
       
   344     ^ (asciivalue < aCharacter asciiValue)
       
   345 !
       
   346 
       
   347 <= aCharacter
       
   348     "return true, if the arguments asciiValue is greater or equal to mine"
       
   349 
       
   350     ^ (asciivalue <= aCharacter asciiValue)
       
   351 !
       
   352 
       
   353 >= aCharacter
       
   354     "return true, if the arguments asciiValue is less or equal to mine"
       
   355 
       
   356     ^ (asciivalue >= aCharacter asciiValue)
       
   357 !
       
   358 
       
   359 identityHash
       
   360     "return an integer useful for hashing on identity"
       
   361 
       
   362     asciivalue <= 255 ifTrue:[
       
   363 	^ 4096 + asciivalue
       
   364     ].
       
   365     ^ super identityHash
       
   366 ! !
       
   367 
       
   368 !Character methodsFor: 'arithmetic'!
       
   369 
   256 
   370 + aMagnitude
   257 + aMagnitude
   371     "Return the Character that is <aMagnitude> higher than the receiver. 
   258     "Return the Character that is <aMagnitude> higher than the receiver. 
   372      Wrap if the resulting value is not a legal Character value. (JS)"
   259      Wrap if the resulting value is not a legal Character value. (JS)"
   373 
   260 
   403      Wrap if the resulting value is not a legal Character value. (JS)"
   290      Wrap if the resulting value is not a legal Character value. (JS)"
   404 
   291 
   405     ^ Character value:(asciivalue \\ aMagnitude asInteger \\ 256)
   292     ^ Character value:(asciivalue \\ aMagnitude asInteger \\ 256)
   406 ! !
   293 ! !
   407 
   294 
   408 !Character methodsFor:'testing'!
   295 !Character methodsFor:'binary storage'!
   409 
   296 
   410 isCharacter
   297 hasSpecialBinaryRepresentation
   411     "return true,  if the receiver is some kind of character"
   298     "return true, if the receiver has a special binary representation"
   412 
   299 
   413     ^ true
   300     ^ true
   414 !
   301 !
   415 
   302 
   416 isDigit
   303 storeBinaryOn:stream manager:manager
   417     "return true, if I am a digit (i.e. $0 .. $9)"
   304     "store a binary representation of the receiver on stream;
   418 
   305      redefined, since single-byte characters are stored more compact
   419     ^ asciivalue between:($0 asciiValue) and:($9 asciiValue)
   306      with a special type-code followed by the asciiValue."
   420 !
   307 
   421 
   308     (asciivalue < 256) ifTrue:[
   422 isDigitRadix:r
   309 	stream nextPut:manager codeForCharacter.
   423     "return true, if I am a digit of a base r number"
   310 	stream nextPut:asciivalue
   424 
   311     ] ifFalse:[
   425     (asciivalue < $0 asciiValue) ifTrue:[^ false]. 
   312 	stream nextPut:manager codeForTwoByteCharacter.
   426     (r > 10) ifTrue:[
   313 	stream nextPutShort:asciivalue MSB:true
   427 	(asciivalue between:($0 asciiValue) and:($9 asciiValue)) ifTrue:[
   314     ]
   428 	    ^ true
   315 ! !
   429 	].
   316 
   430 	((asciivalue - $a asciiValue) between:0 and:(r - 10)) ifTrue:[
   317 !Character methodsFor:'comparing'!
   431 	    ^ true
   318 
   432 	].
   319 < aCharacter
   433 	^ (asciivalue - $A asciiValue) between:0 and:(r - 10)
   320     "return true, if the arguments asciiValue is greater than mine"
       
   321 
       
   322     ^ (asciivalue < aCharacter asciiValue)
       
   323 !
       
   324 
       
   325 <= aCharacter
       
   326     "return true, if the arguments asciiValue is greater or equal to mine"
       
   327 
       
   328     ^ (asciivalue <= aCharacter asciiValue)
       
   329 !
       
   330 
       
   331 = aCharacter
       
   332     "return true, if the argument, aCharacter is the same character
       
   333      Redefined to take care of 16bit characters."
       
   334 
       
   335     self == aCharacter ifTrue:[^ true].
       
   336     aCharacter isCharacter ifFalse:[^ false].
       
   337     ^ (asciivalue = aCharacter asciiValue)
       
   338 !
       
   339 
       
   340 > aCharacter
       
   341     "return true, if the arguments asciiValue is less than mine"
       
   342 
       
   343     ^ (asciivalue > aCharacter asciiValue)
       
   344 !
       
   345 
       
   346 >= aCharacter
       
   347     "return true, if the arguments asciiValue is less or equal to mine"
       
   348 
       
   349     ^ (asciivalue >= aCharacter asciiValue)
       
   350 !
       
   351 
       
   352 identityHash
       
   353     "return an integer useful for hashing on identity"
       
   354 
       
   355     asciivalue <= 255 ifTrue:[
       
   356 	^ 4096 + asciivalue
   434     ].
   357     ].
   435     (asciivalue - $0 asciiValue) < r ifTrue:[^ true].
   358     ^ super identityHash
   436     ^ false
   359 !
   437 !
   360 
   438 
   361 sameAs:aCharacter
   439 isLowercase
   362     "return true, if the argument, aCharacter is the same character,
   440     "return true, if I am a lower-case letter"
   363      ignoring case differences."
   441 
   364 
   442 %{  /* NOCONTEXT */
   365     self == aCharacter ifTrue:[^ true].
   443 
   366     ^ self asLowercase = aCharacter asLowercase
   444     REGISTER int val;
   367 !
   445 
   368 
   446     val = _intVal(_INST(asciivalue));
   369 ~= aCharacter
   447 #ifndef OLD
   370     "return true, if the argument, aCharacter is not the same character
   448     /* iso8859 puts national lower case characters at e0 .. ff */
   371      Redefined to take care of 16bit characters."
   449     if ((val >= 0xE0) && (val <= 0xFF)) {
   372 
   450 	RETURN(true);
   373     self == aCharacter ifTrue:[^ false].
   451     }
   374     aCharacter isCharacter ifFalse:[^ true].
   452 #endif
   375     ^ (asciivalue ~~ aCharacter asciiValue)
   453     RETURN ( ((val >= 'a') && (val <= 'z')) ? true : false );
       
   454 %}
       
   455 !
       
   456 
       
   457 isUppercase
       
   458     "return true, if I am an upper-case letter"
       
   459 
       
   460 %{  /* NOCONTEXT */
       
   461 
       
   462     REGISTER int val;
       
   463 
       
   464     val = _intVal(_INST(asciivalue));
       
   465 #ifndef OLD
       
   466     /* iso8859 puts national upper case characters at c0 .. df */
       
   467     if ((val >= 0xC0) && (val <= 0xDF)) {
       
   468 	RETURN(true);
       
   469     }
       
   470 #endif
       
   471     RETURN ( ((val >= 'A') && (val <= 'Z')) ? true : false );
       
   472 %}
       
   473 !
       
   474 
       
   475 isLetter
       
   476     "return true, if I am a letter 
       
   477      - use isNationalLetter, if you are interrested in those."
       
   478 
       
   479 %{  /*NOCONTEXT */
       
   480 
       
   481     REGISTER int val;
       
   482 
       
   483     val = _intVal(_INST(asciivalue));
       
   484     RETURN ( (((val >= 'a') && (val <= 'z')) ||
       
   485 	      ((val >= 'A') && (val <= 'Z'))) ? true : false );
       
   486 %}
       
   487 !
       
   488 
       
   489 isLetterOrDigit
       
   490     "return true, if I am a letter or a digit
       
   491      - use isNationalAlphaNumeric, if you are interrested in those."
       
   492 
       
   493 %{  /* NOCONTEXT */
       
   494 
       
   495     REGISTER int val;
       
   496 
       
   497     val = _intVal(_INST(asciivalue));
       
   498     if ((val >= 'a') && (val <= 'z')) {
       
   499 	RETURN ( true );
       
   500     }
       
   501     if ((val >= 'A') && (val <= 'Z')) {
       
   502 	RETURN ( true );
       
   503     }
       
   504     if ((val >= '0') && (val <= '9')) {
       
   505 	RETURN ( true );
       
   506     }
       
   507     RETURN ( false );
       
   508 %}
       
   509 !
       
   510 
       
   511 isAlphaNumeric
       
   512     "return true, if I am a letter or a digit
       
   513      - same as isAlphaNumeric for compatibility reasons."
       
   514 
       
   515     ^ self isLetterOrDigit
       
   516 !
       
   517 
       
   518 isVowel
       
   519     "return true, if I am a vowel (lower- or uppercase)"
       
   520 
       
   521     (self == $a) ifTrue:[^ true].
       
   522     (self == $e) ifTrue:[^ true].
       
   523     (self == $i) ifTrue:[^ true].
       
   524     (self == $o) ifTrue:[^ true].
       
   525     (self == $u) ifTrue:[^ true].
       
   526     (self == $A) ifTrue:[^ true].
       
   527     (self == $E) ifTrue:[^ true].
       
   528     (self == $I) ifTrue:[^ true].
       
   529     (self == $O) ifTrue:[^ true].
       
   530     (self == $U) ifTrue:[^ true].
       
   531     ^ false
       
   532 !
       
   533 
       
   534 isSeparator
       
   535     "return true if I am a space, cr, tab, nl, or newPage"
       
   536 
       
   537 %{  /* NOCONTEXT */
       
   538 
       
   539     REGISTER int val;
       
   540 
       
   541     val = _intVal(_INST(asciivalue));
       
   542 #ifndef NON_ASCII       /* i.e. EBCDIC ;-) */
       
   543     if (val <= ' ')
       
   544 #endif
       
   545 	if ((val == ' ')
       
   546 	 || (val == '\n') 
       
   547 	 || (val == '\t')
       
   548 	 || (val == '\r')
       
   549 	 || (val == '\f')) {
       
   550 	    RETURN ( true );
       
   551 	}
       
   552 %}
       
   553 .
       
   554     ^ false
       
   555 !
       
   556 
       
   557 isEndOfLineCharacter
       
   558     "return true if I am a line delimitting character"
       
   559 
       
   560 %{  /* NOCONTEXT */
       
   561 
       
   562     REGISTER int val;
       
   563 
       
   564     val = _intVal(_INST(asciivalue));
       
   565     if ((val == '\n')
       
   566      || (val == '\r')
       
   567      || (val == '\f')) {
       
   568 	RETURN ( true );
       
   569     }
       
   570 %}
       
   571 .
       
   572     ^ false
       
   573 !
       
   574 
       
   575 isPrintable
       
   576     "return true, if the receiver is a useful printable character
       
   577      (see fileBrowsers showFile:-method on how it can be used)"
       
   578 
       
   579     (asciivalue between:32 and:127) ifTrue:[^ true].
       
   580     asciivalue == 13 ifTrue:[^ true].
       
   581     asciivalue == 9 ifTrue:[^ true].
       
   582     asciivalue == 10 ifTrue:[^ true].
       
   583     ^ self isNationalLetter
       
   584 ! !
       
   585 
       
   586 !Character methodsFor:'national testing'!
       
   587 
       
   588 isNationalLetter
       
   589     "return true, if the receiver is a letter in the
       
   590      current language (Language variable)"
       
   591 
       
   592     "stupid - should be configurable from a table ...
       
   593      ... good thing is, that iso8859 puts all national
       
   594 	 characters above 16rC0"
       
   595 
       
   596     self isLetter ifTrue:[^ true].
       
   597     ^ self asciiValue between:16rC0 and:16rFF
       
   598 !
       
   599 
       
   600 isNationalAlphaNumeric
       
   601     "return true, if the receiver is a letter in the
       
   602      current language (Language variable)"
       
   603 
       
   604     "stupid - should be configurable from a table ...
       
   605      ... good thing is, that iso8859 puts all national
       
   606 	 characters above 16rC0"
       
   607 
       
   608     self isLetterOrDigit ifTrue:[^ true].
       
   609     ^ self asciiValue between:16rC0 and:16rFF
       
   610 ! !
   376 ! !
   611 
   377 
   612 !Character methodsFor:'converting'!
   378 !Character methodsFor:'converting'!
       
   379 
       
   380 asCharacter
       
   381     "usually sent to integers, but redefined here to allow integers
       
   382      and characters to be used commonly without a need for a test."
       
   383 
       
   384     ^ self
       
   385 
       
   386     "
       
   387      32 asCharacter  
       
   388     "
       
   389 !
       
   390 
       
   391 asInteger
       
   392     "return an Integer with my ascii-value.
       
   393      OWST4.2 compatibility (sigh)"
       
   394 
       
   395     ^ asciivalue
       
   396 !
   613 
   397 
   614 asLowercase
   398 asLowercase
   615     "return a character with same letter as the receiver,
   399     "return a character with same letter as the receiver,
   616      but lowercase (the receiver if its lowercase or nonLetter)"
   400      but lowercase (the receiver if its lowercase or nonLetter)"
   617 
   401 
   618     self isUppercase ifFalse:[^ self].
   402     self isUppercase ifFalse:[^ self].
   619     ^ Character value:(asciivalue + 32)
   403     ^ Character value:(asciivalue + 32)
   620 !
       
   621 
       
   622 asUppercase
       
   623     "return a character with same letter as the receiver,
       
   624      but uppercase (the receiver if its uppercase or nonLetter)"
       
   625 
       
   626     self isLowercase ifFalse:[^ self].
       
   627     ^ Character value:(asciivalue - 32)
       
   628 !
       
   629 
       
   630 asCharacter
       
   631     "usually sent to integers, but redefined here to allow integers
       
   632      and characters to be used commonly without a need for a test."
       
   633 
       
   634     ^ self
       
   635 
       
   636     "
       
   637      32 asCharacter  
       
   638     "
       
   639 !
       
   640 
       
   641 asInteger
       
   642     "return an Integer with my ascii-value.
       
   643      OWST4.2 compatibility (sigh)"
       
   644 
       
   645     ^ asciivalue
       
   646 !
       
   647 
       
   648 asSymbol
       
   649     "return a unique symbol which prints like I print"
       
   650 
       
   651     ^ Symbol internCharacter:self
       
   652 !
   404 !
   653 
   405 
   654 asString
   406 asString
   655     "return a string of len 1 with myself as contents"
   407     "return a string of len 1 with myself as contents"
   656 
   408 
   680      This means, that the VM wanted to get some more memory from the
   432      This means, that the VM wanted to get some more memory from the
   681      OS, which was not kind enough to give it.
   433      OS, which was not kind enough to give it.
   682      Bad luck - you should increase the swap space on your machine.
   434      Bad luck - you should increase the swap space on your machine.
   683     "
   435     "
   684     ^ ObjectMemory allocationFailureSignal raise.
   436     ^ ObjectMemory allocationFailureSignal raise.
       
   437 !
       
   438 
       
   439 asSymbol
       
   440     "return a unique symbol which prints like I print"
       
   441 
       
   442     ^ Symbol internCharacter:self
       
   443 !
       
   444 
       
   445 asUppercase
       
   446     "return a character with same letter as the receiver,
       
   447      but uppercase (the receiver if its uppercase or nonLetter)"
       
   448 
       
   449     self isLowercase ifFalse:[^ self].
       
   450     ^ Character value:(asciivalue - 32)
   685 !
   451 !
   686 
   452 
   687 digitValue
   453 digitValue
   688     "return my digitValue for any base"
   454     "return my digitValue for any base"
   689 
   455 
   711      Wrap <aMagnitude> if it is not a legal Character value. (JS)"
   477      Wrap <aMagnitude> if it is not a legal Character value. (JS)"
   712 
   478 
   713     ^ Interval from:self to:(aMagnitude \\ 256)
   479     ^ Interval from:self to:(aMagnitude \\ 256)
   714 ! !
   480 ! !
   715 
   481 
       
   482 !Character methodsFor:'copying'!
       
   483 
       
   484 copy
       
   485     "return a copy of myself
       
   486      reimplemented since characters are unique"
       
   487 
       
   488      ^ self
       
   489 !
       
   490 
       
   491 deepCopy
       
   492     "return a deep copy of myself
       
   493      reimplemented since characters are unique"
       
   494 
       
   495      ^ self
       
   496 !
       
   497 
       
   498 deepCopyUsing:aDictionary
       
   499     "return a deep copy of myself
       
   500      reimplemented since characters are unique"
       
   501 
       
   502      ^ self
       
   503 !
       
   504 
       
   505 shallowCopy
       
   506     "return a shallow copy of myself
       
   507      reimplemented since characters are unique"
       
   508 
       
   509      ^ self
       
   510 !
       
   511 
       
   512 simpleDeepCopy
       
   513     "return a deep copy of myself
       
   514      reimplemented since characters are unique"
       
   515 
       
   516      ^ self
       
   517 ! !
       
   518 
   716 !Character methodsFor:'enumerating'!
   519 !Character methodsFor:'enumerating'!
   717 
   520 
   718 to:stopCharacter do:aBlock
   521 to:stopCharacter do:aBlock
   719     "evaluate aBlock for each character in self .. stopCharacter.
   522     "evaluate aBlock for each character in self .. stopCharacter.
   720      This is somewhat stupid, since it depends on the ascii encoding
   523      This is somewhat stupid, since it depends on the ascii encoding
   732      ($a to:$z) do:[:char | char printNL]
   535      ($a to:$z) do:[:char | char printNL]
   733      $a to:$z do:[:char | char printNL].
   536      $a to:$z do:[:char | char printNL].
   734     "
   537     "
   735 ! !
   538 ! !
   736 
   539 
   737 !Character methodsFor:'binary storage'!
   540 !Character methodsFor:'national testing'!
   738 
   541 
   739 hasSpecialBinaryRepresentation
   542 isNationalAlphaNumeric
   740     "return true, if the receiver has a special binary representation"
   543     "return true, if the receiver is a letter in the
   741 
   544      current language (Language variable)"
   742     ^ true
   545 
   743 !
   546     "stupid - should be configurable from a table ...
   744 
   547      ... good thing is, that iso8859 puts all national
   745 storeBinaryOn:stream manager:manager
   548 	 characters above 16rC0"
   746     "store a binary representation of the receiver on stream;
   549 
   747      redefined, since single-byte characters are stored more compact
   550     self isLetterOrDigit ifTrue:[^ true].
   748      with a special type-code followed by the asciiValue."
   551     ^ self asciiValue between:16rC0 and:16rFF
   749 
   552 !
   750     (asciivalue < 256) ifTrue:[
   553 
   751 	stream nextPut:manager codeForCharacter.
   554 isNationalLetter
   752 	stream nextPut:asciivalue
   555     "return true, if the receiver is a letter in the
   753     ] ifFalse:[
   556      current language (Language variable)"
   754 	stream nextPut:manager codeForTwoByteCharacter.
   557 
   755 	stream nextPutShort:asciivalue MSB:true
   558     "stupid - should be configurable from a table ...
   756     ]
   559      ... good thing is, that iso8859 puts all national
       
   560 	 characters above 16rC0"
       
   561 
       
   562     self isLetter ifTrue:[^ true].
       
   563     ^ self asciiValue between:16rC0 and:16rFF
   757 ! !
   564 ! !
   758 
   565 
   759 !Character methodsFor:'printing & storing'!
   566 !Character methodsFor:'printing & storing'!
       
   567 
       
   568 displayString
       
   569     "return a string used when the receiver is to be displayed
       
   570      in an inspector kind-of-thing"
       
   571 
       
   572     ^ self storeString
       
   573 !
   760 
   574 
   761 isLiteral
   575 isLiteral
   762     "return true, if the receiver can be used as a literal
   576     "return true, if the receiver can be used as a literal
   763      (i.e. can be used in constant arrays)"
   577      (i.e. can be used in constant arrays)"
   764 
   578 
   765     ^ true
   579     ^ true
   766 !
   580 !
   767 
   581 
       
   582 print
       
   583     "print myself on stdout"
       
   584 
       
   585 %{  /* NOCONTEXT */
       
   586 
       
   587     putchar(_intVal(_INST(asciivalue)));
       
   588 %}
       
   589 !
       
   590 
       
   591 printOn:aStream
       
   592     "print myself on aStream"
       
   593 
       
   594     aStream nextPut:self
       
   595 !
       
   596 
   768 printString
   597 printString
   769     "return a string to print me"
   598     "return a string to print me"
   770 
   599 
   771     ^ self asString
   600     ^ self asString
   772 !
       
   773 
       
   774 printOn:aStream
       
   775     "print myself on aStream"
       
   776 
       
   777     aStream nextPut:self
       
   778 !
       
   779 
       
   780 print
       
   781     "print myself on stdout"
       
   782 
       
   783 %{  /* NOCONTEXT */
       
   784 
       
   785     putchar(_intVal(_INST(asciivalue)));
       
   786 %}
       
   787 !
       
   788 
       
   789 displayString
       
   790     "return a string used when the receiver is to be displayed
       
   791      in an inspector kind-of-thing"
       
   792 
       
   793     ^ self storeString
       
   794 !
   601 !
   795 
   602 
   796 storeOn:aStream
   603 storeOn:aStream
   797     "store myself on aStream"
   604     "store myself on aStream"
   798 
   605 
   818 		nextPutAll:(asciivalue printString); nextPutAll:')'
   625 		nextPutAll:(asciivalue printString); nextPutAll:')'
   819     ] ifTrue:[
   626     ] ifTrue:[
   820 	aStream nextPut:$$; nextPut:self
   627 	aStream nextPut:$$; nextPut:self
   821     ]
   628     ]
   822 ! !
   629 ! !
       
   630 
       
   631 !Character methodsFor:'private accessing'!
       
   632 
       
   633 setAsciiValue:anInteger
       
   634     "very private - set the ascii value. 
       
   635      - use this only for characters with codes > 16rFF.
       
   636      DANGER alert: funny things happen, if this is applied to
       
   637      one of the fixed-characters 0..255."
       
   638 
       
   639     asciivalue := anInteger
       
   640 ! !
       
   641 
       
   642 !Character methodsFor:'testing'!
       
   643 
       
   644 isAlphaNumeric
       
   645     "return true, if I am a letter or a digit
       
   646      - same as isAlphaNumeric for compatibility reasons."
       
   647 
       
   648     ^ self isLetterOrDigit
       
   649 !
       
   650 
       
   651 isCharacter
       
   652     "return true,  if the receiver is some kind of character"
       
   653 
       
   654     ^ true
       
   655 !
       
   656 
       
   657 isDigit
       
   658     "return true, if I am a digit (i.e. $0 .. $9)"
       
   659 
       
   660     ^ asciivalue between:($0 asciiValue) and:($9 asciiValue)
       
   661 !
       
   662 
       
   663 isDigitRadix:r
       
   664     "return true, if I am a digit of a base r number"
       
   665 
       
   666     (asciivalue < $0 asciiValue) ifTrue:[^ false]. 
       
   667     (r > 10) ifTrue:[
       
   668 	(asciivalue between:($0 asciiValue) and:($9 asciiValue)) ifTrue:[
       
   669 	    ^ true
       
   670 	].
       
   671 	((asciivalue - $a asciiValue) between:0 and:(r - 10)) ifTrue:[
       
   672 	    ^ true
       
   673 	].
       
   674 	^ (asciivalue - $A asciiValue) between:0 and:(r - 10)
       
   675     ].
       
   676     (asciivalue - $0 asciiValue) < r ifTrue:[^ true].
       
   677     ^ false
       
   678 !
       
   679 
       
   680 isEndOfLineCharacter
       
   681     "return true if I am a line delimitting character"
       
   682 
       
   683 %{  /* NOCONTEXT */
       
   684 
       
   685     REGISTER int val;
       
   686 
       
   687     val = _intVal(_INST(asciivalue));
       
   688     if ((val == '\n')
       
   689      || (val == '\r')
       
   690      || (val == '\f')) {
       
   691 	RETURN ( true );
       
   692     }
       
   693 %}
       
   694 .
       
   695     ^ false
       
   696 !
       
   697 
       
   698 isLetter
       
   699     "return true, if I am a letter 
       
   700      - use isNationalLetter, if you are interrested in those."
       
   701 
       
   702 %{  /*NOCONTEXT */
       
   703 
       
   704     REGISTER int val;
       
   705 
       
   706     val = _intVal(_INST(asciivalue));
       
   707     RETURN ( (((val >= 'a') && (val <= 'z')) ||
       
   708 	      ((val >= 'A') && (val <= 'Z'))) ? true : false );
       
   709 %}
       
   710 !
       
   711 
       
   712 isLetterOrDigit
       
   713     "return true, if I am a letter or a digit
       
   714      - use isNationalAlphaNumeric, if you are interrested in those."
       
   715 
       
   716 %{  /* NOCONTEXT */
       
   717 
       
   718     REGISTER int val;
       
   719 
       
   720     val = _intVal(_INST(asciivalue));
       
   721     if ((val >= 'a') && (val <= 'z')) {
       
   722 	RETURN ( true );
       
   723     }
       
   724     if ((val >= 'A') && (val <= 'Z')) {
       
   725 	RETURN ( true );
       
   726     }
       
   727     if ((val >= '0') && (val <= '9')) {
       
   728 	RETURN ( true );
       
   729     }
       
   730     RETURN ( false );
       
   731 %}
       
   732 !
       
   733 
       
   734 isLowercase
       
   735     "return true, if I am a lower-case letter"
       
   736 
       
   737 %{  /* NOCONTEXT */
       
   738 
       
   739     REGISTER int val;
       
   740 
       
   741     val = _intVal(_INST(asciivalue));
       
   742 #ifndef OLD
       
   743     /* iso8859 puts national lower case characters at e0 .. ff */
       
   744     if ((val >= 0xE0) && (val <= 0xFF)) {
       
   745 	RETURN(true);
       
   746     }
       
   747 #endif
       
   748     RETURN ( ((val >= 'a') && (val <= 'z')) ? true : false );
       
   749 %}
       
   750 !
       
   751 
       
   752 isPrintable
       
   753     "return true, if the receiver is a useful printable character
       
   754      (see fileBrowsers showFile:-method on how it can be used)"
       
   755 
       
   756     (asciivalue between:32 and:127) ifTrue:[^ true].
       
   757     asciivalue == 13 ifTrue:[^ true].
       
   758     asciivalue == 9 ifTrue:[^ true].
       
   759     asciivalue == 10 ifTrue:[^ true].
       
   760     ^ self isNationalLetter
       
   761 !
       
   762 
       
   763 isSeparator
       
   764     "return true if I am a space, cr, tab, nl, or newPage"
       
   765 
       
   766 %{  /* NOCONTEXT */
       
   767 
       
   768     REGISTER int val;
       
   769 
       
   770     val = _intVal(_INST(asciivalue));
       
   771 #ifndef NON_ASCII       /* i.e. EBCDIC ;-) */
       
   772     if (val <= ' ')
       
   773 #endif
       
   774 	if ((val == ' ')
       
   775 	 || (val == '\n') 
       
   776 	 || (val == '\t')
       
   777 	 || (val == '\r')
       
   778 	 || (val == '\f')) {
       
   779 	    RETURN ( true );
       
   780 	}
       
   781 %}
       
   782 .
       
   783     ^ false
       
   784 !
       
   785 
       
   786 isUppercase
       
   787     "return true, if I am an upper-case letter"
       
   788 
       
   789 %{  /* NOCONTEXT */
       
   790 
       
   791     REGISTER int val;
       
   792 
       
   793     val = _intVal(_INST(asciivalue));
       
   794 #ifndef OLD
       
   795     /* iso8859 puts national upper case characters at c0 .. df */
       
   796     if ((val >= 0xC0) && (val <= 0xDF)) {
       
   797 	RETURN(true);
       
   798     }
       
   799 #endif
       
   800     RETURN ( ((val >= 'A') && (val <= 'Z')) ? true : false );
       
   801 %}
       
   802 !
       
   803 
       
   804 isVowel
       
   805     "return true, if I am a vowel (lower- or uppercase)"
       
   806 
       
   807     (self == $a) ifTrue:[^ true].
       
   808     (self == $e) ifTrue:[^ true].
       
   809     (self == $i) ifTrue:[^ true].
       
   810     (self == $o) ifTrue:[^ true].
       
   811     (self == $u) ifTrue:[^ true].
       
   812     (self == $A) ifTrue:[^ true].
       
   813     (self == $E) ifTrue:[^ true].
       
   814     (self == $I) ifTrue:[^ true].
       
   815     (self == $O) ifTrue:[^ true].
       
   816     (self == $U) ifTrue:[^ true].
       
   817     ^ false
       
   818 ! !
       
   819 
       
   820 !Character class methodsFor:'documentation'!
       
   821 
       
   822 version
       
   823     ^' $Header: /cvs/stx/stx/libbasic/Character.st,v 1.27 1995-12-07 21:31:57 cg Exp $'
       
   824 ! !