"""Parse link destination"""from..common.utilsimportunescapeAll,charCodeAtclass_Result:__slots__=("ok","pos","lines","str")def__init__(self):self.ok=Falseself.pos=0self.lines=0self.str=""
[文档]defparseLinkDestination(string:str,pos:int,maximum:int)->_Result:lines=0start=posresult=_Result()ifcharCodeAt(string,pos)==0x3C:# /* < */pos+=1whilepos<maximum:code=charCodeAt(string,pos)ifcode==0x0A:# /* \n */)returnresultifcode==0x3C:# / * < * /returnresultifcode==0x3E:# /* > */) {result.pos=pos+1result.str=unescapeAll(string[start+1:pos])result.ok=Truereturnresultifcode==0x5Candpos+1<maximum:# \pos+=2continuepos+=1# no closing '>'returnresult# this should be ... } else { ... branchlevel=0whilepos<maximum:code=charCodeAt(string,pos)ifcode==0x20:break# ascii control charactersifcode<0x20orcode==0x7F:breakifcode==0x5Candpos+1<maximum:ifcharCodeAt(string,pos+1)==0x20:breakpos+=2continueifcode==0x28:# /* ( */)level+=1iflevel>32:returnresultifcode==0x29:# /* ) */)iflevel==0:breaklevel-=1pos+=1ifstart==pos:returnresultiflevel!=0:returnresultresult.str=unescapeAll(string[start:pos])result.lines=linesresult.pos=posresult.ok=Truereturnresult