How to build a String instance from a sequence of integers?

I would like to create a test string from Unicode code points

Something like that

 65 asCharacter asString,
 66 asCharacter asString,
 67 asCharacter asString,
 65 asCharacter asString,
769 asCharacter asString

or

String with: 65 asCharacter
       with: 66 asCharacter
       with: 67 asCharacter
       with: 65 asCharacter
       with: 769 asCharacter

It works, but

I am looking for a way to convert an array of integer values ​​to an instance of the String class.

#(65 66 67 65 769)

Is there a built-in method for this? I am looking for such an answer. What is the correct way to test for Unicode support in a Smalltalk implementation? one but for strings.

+4
source share
4 answers

Many ways

1. #streamContents:

, /, . , , .

String streamContents: [ :aStream |
    #(65 66 67 65 769) do: [ :each |
        aStream nextPut: each asCharacter
    ]
]

String streamContents: [ :aStream |
    aStream nextPutAll: (#(65 66 67 65 769) collect: #asCharacter)
]

2. #withAll:

String withAll: (#(65 66 67 65 769) collect: #asCharacter)

3. #collect: as: String

#(65 66 67 65 769) collect: #asCharacter as: String

4. #joinUsing:

(#(65 66 67 65 769) collect: #asCharacter) joinUsing: ''

:

, Pharo [ :each | each selector ], #selector. , , .

+7

String #withAll:

String withAll: 
   (#(65 66 67 65 769) collect: [:codepoint | codepoint asCharacter])
+4

" ":

codepoints := #(65 66 67 65 769).

string := WideString new: codepoints size.
codepoints withIndexDo: [:cp :i | string wordAt: i put: cp].
^string
+1

, , , , , !
, , :

'' asWideString copyReplaceFrom: 1 to: 0 with: (#(65 66 67 65 769) as: WordArray).

, , , , VariableWord...

( WriteStream - ) :

^'' asWideString writeStream
    nextPutAll: (#(65 66 67 65 769) as: WordArray);
    contents

ByteString ByteArray.

, , , , BitBlt:

^((BitBlt toForm: (Form new hackBits: (WideString new: 5)))
    sourceForm: (Form new hackBits: (#(65 66 67 65 769) as: WordArray));
    combinationRule: Form over;
    copyBits;
    destForm) bits

WordArray WideString, ( ).

, , !

+1

Source: https://habr.com/ru/post/1619569/


All Articles