# For each opening emphasis-like marker find a matching closing one#from.state_inlineimportStateInline
[文档]defprocessDelimiters(state:StateInline,delimiters,*args):openersBottom={}maximum=len(delimiters)closerIdx=0whilecloserIdx<maximum:closer=delimiters[closerIdx]# Length is only used for emphasis-specific "rule of 3",# if it's not defined (in strikethrough or 3rd party plugins),# we can default it to 0 to disable those checks.#closer.length=closer.lengthor0ifnotcloser.close:closerIdx+=1continue# Previously calculated lower bounds (previous fails)# for each marker, each delimiter length modulo 3,# and for whether this closer can be an opener;# https://github.com/commonmark/cmark/commit/34250e12ccebdc6372b8b49c44fab57c72443460ifcloser.markernotinopenersBottom:openersBottom[closer.marker]=[-1,-1,-1,-1,-1,-1]minOpenerIdx=openersBottom[closer.marker][(3ifcloser.openelse0)+(closer.length%3)]openerIdx=closerIdx-closer.jump-1# avoid crash if `closer.jump` is pointing outside of the array,# e.g. for strikethroughifopenerIdx<-1:openerIdx=-1newMinOpenerIdx=openerIdxwhileopenerIdx>minOpenerIdx:opener=delimiters[openerIdx]ifopener.marker!=closer.marker:openerIdx-=opener.jump+1continueifopener.openandopener.end<0:isOddMatch=False# from spec:## If one of the delimiters can both open and close emphasis, then the# sum of the lengths of the delimiter runs containing the opening and# closing delimiters must not be a multiple of 3 unless both lengths# are multiples of 3.#ifopener.closeorcloser.open:if(opener.length+closer.length)%3==0:ifopener.length%3!=0orcloser.length%3!=0:isOddMatch=TrueifnotisOddMatch:# If previous delimiter cannot be an opener, we can safely skip# the entire sequence in future checks. This is required to make# sure algorithm has linear complexity (see *_*_*_*_*_... case).#ifopenerIdx>0andnotdelimiters[openerIdx-1].open:lastJump=delimiters[openerIdx-1].jump+1else:lastJump=0closer.jump=closerIdx-openerIdx+lastJumpcloser.open=Falseopener.end=closerIdxopener.jump=lastJumpopener.close=FalsenewMinOpenerIdx=-1breakopenerIdx-=opener.jump+1ifnewMinOpenerIdx!=-1:# If match for this delimiter run failed, we want to set lower bound for# future lookups. This is required to make sure algorithm has linear# complexity.## See details here:# https:#github.com/commonmark/cmark/issues/178#issuecomment-270417442#openersBottom[closer.marker][(3ifcloser.openelse0)+((closer.lengthor0)%3)]=newMinOpenerIdxcloserIdx+=1