[squeak-dev] Unicode

christoph.thiede at student.hpi.uni-potsdam.de christoph.thiede at student.hpi.uni-potsdam.de
Thu Feb 24 21:16:25 UTC 2022


Hi all, Hi Marcel, Hi Levente,

finally, here is a changeset that takes the first step for updating or in-image Unicode database. After filing it in, please run:

	Unicode initializeUnicodeData.

In addition to the preamble (please read first below), I have still a number of questions:

* Is it okay to fetch the data from unicode.org via a postscript in the update stream? Hypothetically, some clients might use a proxy or a strict firewall/safelist of IP addresses.
* How much effort shall we put in deduplicating the logic and the data in this class? This includes both the two similar parsing methods and the redundant specification of the Unicode character tags.

Best,
Christoph

=============== Summary ===============

Change Set:		UnicodeData
Date:			24 February 2022
Author:			Christoph Thiede

This changeset repairs the fetching & parsing of unicode category data. Usage:

	Unicode initializeUnicodeData.
	Unicode generalCategoryLabelOf: 16r1F388. 'Symbol, Other'

This revision resolves some slips in the category tags, adds an interface for retrieving/converting tags, unifies the vocabulary of the Unicode protocol, integrates the #initializeUnicodeData into the class initializer, and adds some tests.

Still present limitations include:
- Duplication between #parseUnicodeDataFrom: and #parseCompositionMappingFrom:
- Redundant and scattered declaration of character categories

For more information, see: http://lists.squeakfoundation.org/pipermail/squeak-dev/2020-March/208020.html

=============== Postscript ===============

"Postscript:
Leave the line above, and replace the rest of this comment by a useful one.
Executable statements should follow this comment, and should
be separated by periods, with no exclamation points (!).
Be sure to put any further comments in double-quotes, like this one."


=============== Diff ===============

Unicode class>>allCategoryTags {character classification} · ct 2/24/2022 19:41
+ allCategoryTags
+ 
+ 	^ #(Cn Cc Cf Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs)

Unicode class>>generalCategoryIndexFromTag: {character classification} · ct 2/24/2022 19:33
+ generalCategoryIndexFromTag: tag
+ 
+ 	^ (self allCategoryTags indexOf: tag) - 1

Unicode class>>generalCategoryLabelForTag: {character classification} · ct 2/24/2022 21:47
+ generalCategoryLabelForTag: tag
+ 
+ 	^ self generalCategoryLabels at: (self generalCategoryIndexFromTag: tag) + 1

Unicode class>>generalCategoryTagOf: {character classification} · ct 2/24/2022 19:44
+ generalCategoryTagOf: aCharacterCode
+ 
+ 	^ (self generalCategoryOf: aCharacterCode)
+ 		ifNotNil: [:code | self allCategoryTags at: code + 1 ifAbsent: [#Cn]]
+ 		ifNil: [#Cn]

Unicode class>>initialize {class initialization} · ct 2/24/2022 21:43 (changed)
initialize
	" Unicode initialize "
	self initializeTagConstants.
- 	Compositions isEmptyOrNil ifTrue:[self initializeCompositionMappings].
+ 	Compositions isEmptyOrNil ifTrue:[self initializeCompositionMappings].
+ 	GeneralCategory isEmptyOrNil ifTrue: [self initializeUnicodeData].

Unicode class>>initializeUnicodeData {unicode data} · ct 2/24/2022 19:03
+ initializeUnicodeData
+ 	"self initializeUnicodeData"
+ 
+ 	self parseUnicodeDataFrom: self unicodeData readStream.

Unicode class>>parseUnicodeDataFrom: {unicode data} · ct 2/24/2022 19:33 (changed)
parseUnicodeDataFrom: stream
- "
- 	self halt.
- 	self parseUnicodeDataFile
- "
+ 	"self initializeUnicodeData."

- 	| line fieldEnd point fieldStart toNumber generalCategory decimalProperty |
+ 	| line fieldEnd point fieldStart toNumber generalCategory decimalProperty tag |

	toNumber := [:quad | ('16r', quad) asNumber].

	GeneralCategory := SparseLargeTable new: 16rE0080 chunkSize: 1024 arrayClass: Array base: 1 defaultValue:  'Cn'.
	DecimalProperty := SparseLargeTable new: 16rE0080 chunkSize: 32 arrayClass: Array base: 1 defaultValue: -1.

	16r3400 to: 16r4DB5 do: [:i | GeneralCategory at: i+1 put: 'Lo'].
	16r4E00 to: 16r9FA5 do: [:i | GeneralCategory at: i+1 put: 'Lo'].
	16rAC00 to: 16rD7FF do: [:i | GeneralCategory at: i+1 put: 'Lo'].

	[(line := stream nextLine) size > 0] whileTrue: [
		fieldEnd := line indexOf: $; startingAt: 1.
		point := toNumber value: (line copyFrom: 1 to: fieldEnd - 1).
		point > 16rE007F ifTrue: [
			GeneralCategory zapDefaultOnlyEntries.
			DecimalProperty zapDefaultOnlyEntries.
			^ self].
		2 to: 3 do: [:i |
			fieldStart := fieldEnd + 1.
			fieldEnd := line indexOf: $; startingAt: fieldStart.
		].
- 		generalCategory := line copyFrom: fieldStart to: fieldEnd - 1.
+ 		tag := line copyFrom: fieldStart to: fieldEnd - 1.
+ 		generalCategory := self generalCategoryIndexFromTag: tag.
		GeneralCategory at: point+1 put: generalCategory.
- 		generalCategory = 'Nd' ifTrue: [
+ 		generalCategory = Nd ifTrue: [
			4 to: 7 do: [:i |
				fieldStart := fieldEnd + 1.
				fieldEnd := line indexOf: $; startingAt: fieldStart.
			].
			decimalProperty :=  line copyFrom: fieldStart to: fieldEnd - 1.
			DecimalProperty at: point+1 put: decimalProperty asNumber.
		].
	].
	GeneralCategory zapDefaultOnlyEntries.
	DecimalProperty zapDefaultOnlyEntries.


UnicodeTest
+ TestCase subclass: #UnicodeTest
+ 	instanceVariableNames: ''
+ 	classVariableNames: ''
+ 	poolDictionaries: ''
+ 	category: 'MultilingualTests-Encodings'
+ 
+ UnicodeTest class 
+ 	instanceVariableNames: ''
+ 
+ ""

UnicodeTest class>>resources {accessing} · ct 2/24/2022 21:46
+ resources
+ 
+ 	 ^ super resources copyWith: UnicodeTestResource

UnicodeTest>>testGeneralCategoryLabel {tests - character classification} · ct 2/24/2022 21:49
+ testGeneralCategoryLabel
+ 
+ 	self assert: 'Letter, Lowercase' equals: (Unicode generalCategoryLabelOf: $a asUnicode).
+ 	self assert: 'Letter, Uppercase' equals: (Unicode generalCategoryLabelOf: $Z asUnicode).
+ 	
+ 	self assert: 'Number, Decimal' equals: (Unicode generalCategoryLabelOf: $5 asUnicode).
+ 	self assert: 'Symbol, Other' equals: (Unicode generalCategoryLabelOf: 16r1F388).
+ 	
+ 	self assert: 'n/a' equals: (Unicode generalCategoryLabelOf: Float infinity).

UnicodeTest>>testGeneralCategoryLabelForTag {tests - character classification} · ct 2/24/2022 21:48
+ testGeneralCategoryLabelForTag
+ 
+ 	self assert: 'Letter, Lowercase' equals: (Unicode generalCategoryLabelForTag: #Ll).

UnicodeTest>>testGeneralCategoryTag {tests - character classification} · ct 2/24/2022 21:49
+ testGeneralCategoryTag
+ 
+ 	self assert: #Ll equals: (Unicode generalCategoryTagOf: $a asUnicode).
+ 	self assert: #Lu equals: (Unicode generalCategoryTagOf: $Z asUnicode).
+ 	
+ 	self assert: #Nd equals: (Unicode generalCategoryTagOf: $5 asUnicode).
+ 	self assert: #So equals: (Unicode generalCategoryTagOf: 16r1F388).
+ 	
+ 	self assert: #Cn equals: (Unicode generalCategoryTagOf: Float infinity).

UnicodeTestResource
+ TestResource subclass: #UnicodeTestResource
+ 	instanceVariableNames: ''
+ 	classVariableNames: ''
+ 	poolDictionaries: ''
+ 	category: 'MultilingualTests-Encodings'
+ 
+ UnicodeTestResource class 
+ 	instanceVariableNames: ''
+ 
+ ""

UnicodeTestResource>>setUp {running} · ct 2/24/2022 21:45
+ setUp
+ 
+ 	super setUp.
+ 	
+ 	"Test the functionality of this update logic"
+ 	Unicode initializeCompositionMappings.
+ 	Unicode initializeUnicodeData.

---
Sent from Squeak Inbox Talk

On 2020-09-11T00:49:24+02:00, leves at caesar.elte.hu wrote:

> Hi Christoph,
> 
> On Wed, 9 Sep 2020, Thiede, Christoph wrote:
> 
> > 
> > Hi Levente,
> > 
> > 
> > basically, I only would like to get rid of the class variables for every single Unicode category because they provide low explorability (it's hard to work with the numeric output of #generalCategoryOf:) and extensibility (you
> > need to recompile the class definition for adding UTF-16 support). If you are critical of increasing the size of the SparseLargeTable, I think we would also just make one or two extra dictionaries to map every category symbol
> > to a number and vice versa. What do you think?
> 
> You mean an array to map the integers to symbols, right? :)
> Anyway, I don't think it's worth using symbols internally.
> For example, #isLetterCode: is 8-10% slower with the extra array lookup 
> and checking the category symbol's first letter than the current method 
> of integer comparisons.
> 
> Do you expect these constants to appear outside the Unicode class? If yes, 
> then using symbols for those cases is probably a good solution.
> But for internal use, the integers are better.
> 
> 
> Levente
> 
> > 
> > 
> > Best,
> > 
> > Christoph
> > 
> > 
> > _________________________________________________________________________________________________________________________________________________________________________________________________________________________________
> > Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Levente Uzonyi <leves at caesar.elte.hu>
> > Gesendet: Dienstag, 8. September 2020 21:43:56
> > An: The general-purpose Squeak developers list
> > Betreff: Re: [squeak-dev] Unicode  
> > Hi Christoph,
> > 
> > On Tue, 8 Sep 2020, Thiede, Christoph wrote:
> > 
> > >
> > > Hi all,
> > >
> > >
> > > > Your words suggest that it has already been published, but I can't find it anywhere.
> > >
> > >
> > > Then I must have expressed myself wrong. I did not yet publish any code changes, but in my original post from March, you can find a short description of the design changes I'd like to implement. Essentially, I would like to
> > > replace the separate class variables for every known character class in favor of greater flexibility.
> > 
> > How would your changes affect GeneralCategory? Would it still be a
> > SpareLargeTable with ByteArray as arrayClass?
> > If you just replace those integers with symbols, the size of the table
> > will be at least 4 or 8 times larger in 32 or 64 bit images,
> > respectively.
> > 
> > 
> > Levente.
> > 
> > >
> > > Eliot, are there any remaining questions regarding the VM size? Character size should be sufficient as discussed below, and of course, I can test any changes in a 32-bit image, too. :-)
> > >
> > > WideString withAll: (#(42r2l 16r1F497 16r1F388 33) collect: #asCharacter)
> > >
> > > Best,
> > > Christoph
> > >
> > >________________________________________________________________________________________________________________________________________________________________________________________________________________________________
> > _
> > > Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Tobias Pape <Das.Linux at gmx.de>
> > > Gesendet: Sonntag, 6. September 2020 21:00:14
> > > An: The general-purpose Squeak developers list
> > > Betreff: Re: [squeak-dev] Unicode  
> > >
> > > > On 06.09.2020, at 20:40, Levente Uzonyi <leves at caesar.elte.hu> wrote:
> > > >
> > > > On Sun, 6 Sep 2020, Tobias Pape wrote:
> > > >
> > > >>
> > > >>> On 06.09.2020, at 19:15, Eliot Miranda <eliot.miranda at gmail.com> wrote:
> > > >>> Hi Christoph, Hi All,
> > > >>>> On Mar 17, 2020, at 3:51 PM, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:
> > > >>>> Hi all! :-)
> > > >>>> After some recent fun with the Unicode class, I found out that its data is quite out of date (for example, the comments do not even "know" that code points can be longer than 4 bytes. Younger characters such as ???
> > > are not categorized correctly, etc. ...). Luckily, there is already some logic to fetch the latest data from www.unicode.org. I'm currently reworking this logic because it's not completely automated yet and has some slips,
> > > but so long, I have one general question for you:
> > > >>> And consequently I have a couple of questions for you. In the Spur VM Characters are immediate (they are like SmallInteger and exist in oops (object-oriented pointers) as tagged values).  In the 32-bit variant
> > Characters
> > > are 30-bit unsigned integers.  In the 64-bit variant they are also 30-bit unsigned integers, but could easily be extended to be up to 61-bit unsigned integers.
> > > >>> Q1, can you arrange that the Unicode support does not break in initialization on the 32-bit variant?  It may be that the 32-bit variant cannot represent code points beyond 30 bits in size, but we should try to ensure
> > that
> > > initialization still runs to completion even if it fails to initialize information relating to code points beyond 30 bits in size.
> > > >>> Q2, how many bits should the 64-bit variant VM support for immediate Characters?
> > > >>
> > > >> Unicode has a max value of 0x10FFFF. That makes 21 bit. So no worries there.
> > > >>
> > > >> We should just not forget the leading-char stuff (Yoshiki, Andreas,...)
> > > >
> > > > AFAIU the leading char only makes sense when you have multiple CJK(V?) languages in use at the same time. In other cases Unicode (leadingChar = 0) is perfectly fine.
> > > > IIRC there are 22 bits available for the codePoint and 8 for the leadingChar, so we're still good: all unicode characters fit.
> > > >
> > > >
> > >
> > > \o/ hooray!
> > >
> > > > Levente
> > > >
> > > >>
> > > >>
> > > >> BEst regards
> > > >>       -Tobias
> > > >>
> > > >>> Then something to consider is that it is conceptually possible to support something like WideCharacter, which would represent code points outside of the immediate Character range on the 32-bit variant, analogous to
> > > LargePositiveInteger beyond SmallInteger maxVal.  This can be made to work seamlessly, just as it does currently with integers, and with Floats where SmallFloat64 is only used on 64-bits.
> > > >>> It has implications in a few parts of the system:
> > > >>> - failure code for WideString (VeryWideString?) at:[put:] primitives that would have to manage overflow into/access from WideCharacter instances
> > > >>> - ImageSegment and other (un)pickling systems that need to convert to/from a bit-specific ?wire? protocol/representation
> > > >>> - 32-bit <=> 64-bit image conversion All this is easily doable (because we have models of doing it for Float and Integer general instances).  But we need good specifications so we can implement the right thing from the
> > > get-go.
> > > >>>> At the moment, we have 30 class variables each for one Unicode category number. These class vars map in alphabetical order to the integers from 0 to: 29. Is this tedious structure really necessary? For different
> > > purposes, I would like to get the category name of a specific code point from a client. The current design makes this impossible without writing additional mappings.
> > > >>>> Tl;dr: I would like to propose to drop these class variables and use Symbols instead. They are comparable like integers, and as they are flyweights, this should not be a performance issue either. Of course,
> > > #generalCategoryOf: will have to keep returning numbers, but we could deprecate it and use a new #generalTagOf: in the future. Furthermore, this would also allow us to deal with later added category names (though I don't
> > know
> > > whether this will ever happen).
> > > >>>> Examples:
> > > >>>> Unicode generalTagOf: $a asUnicode. "#Ll"
> > > >>>> Unicode class >> isLetterCode: charCode
> > > >>>>  ^ (self generalTagOf: charCode) first = $L
> > > >>>> Unicode class >> isAlphaNumericCode: charCode
> > > >>>>  | tag|
> > > >>>>  ^ (tag := self generalCategoryOf: charCode) first = $L
> > > >>>>        or: [tag = #Nd]
> > > >>>> How do you think about this proposal? Please let me know and I will go ahead! :D
> > > >>>> Best,
> > > >>>> Christoph
> > > >>> Best, Eliot
> > > >>> _,,,^..^,,,_ (phone)
> > >
> > >
> > >
> > >
> > >
> > 
> >
> 
> 
["UnicodeData.2.cs"]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20220224/451696fc/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: UnicodeData.2.cs
Type: application/octet-stream
Size: 6186 bytes
Desc: not available
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20220224/451696fc/attachment.obj>


More information about the Squeak-dev mailing list