Microdown icon indicating copy to clipboard operation
Microdown copied to clipboard

MicInlineDelimiter logic can be optimized

Open Ducasse opened this issue 1 year ago • 0 comments

MicInlineDelimiter is used for parsing token

tokenize: rawString
	| inlineString splits from |
	inlineString := self class backslashEncode: rawString.
	splits := MicInlineDelimiter allRegex matchingRangesIn: inlineString.
	tokens := OrderedCollection new.
	from := 1.
	splits do: [ :delMatch |
		self addText: (inlineString copyFrom: from to: delMatch first - 1).
		self addDelimiter: (inlineString copyFrom: delMatch first to: delMatch last).
		from := delMatch last + 1
		 ].
	from <= inlineString size ifTrue: [ self addText: (inlineString copyFrom: from to: inlineString size) ].
	^ ReadStream on: tokens

in particular allRegex

allRegex
	^ ((MicInlineDelimiter all collect: [ :each | each markupAsRegex]) joinUsing: '|') asRegex
markupAsRegex
	"return a regex (as string) matching this delimiter"
	| str |
	str := WriteStream on: ''. 
	self isRawkind
		ifTrue: [ 
				markup do: [ :char| str nextPut: $\; nextPut: char ].
				str nextPutAll: (self class regexNot: closer).
				closer do: [ :char| str nextPut: $\; nextPut: char ] ]
		ifFalse: [ markup do: [ :char| str nextPut: $\; nextPut: char ] ].
	^ str contents

So it means that while MicInlineDelimiter are stored in a dictionary their regex version is continuously recreated.

A solution is to define the regex as an instance variable. And to compute it before storing it during the initialization phase 9in the initializeDelimiters method. For example the addMe could just to it automatically such as

initializeDelimiters

	"The fixious text delimiter"
	self new markup: FixiousTextDelimiter; blockClass: MicTextBlock; closer: nil; addMe.

	"formating"
	self new markup: BoldMarkup; blockClass: MicBoldFormatBlock; closer: BoldMarkup; addMe.
	self new markup: ItalicMarkup; blockClass: MicItalicFormatBlock; closer: ItalicMarkup; addMe.
	self new markup: StrikeMarkup; blockClass: MicStrikeFormatBlock; closer: StrikeMarkup; addMe.
addMe
	"add me to the dictionary in my class"
	DelimiterDictionary at: self markup put: self

=>

addMe
	"add me to the dictionary in my class"
        self storeRegex.
	DelimiterDictionary at: self markup put: self 

Ducasse avatar Mar 23 '24 12:03 Ducasse