[H2] Uri Encode/Decode

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效
arcticir
Posts: 542
Joined: 17 Nov 2013, 11:32

[H2] Uri Encode/Decode

Post by arcticir » 06 Mar 2016, 08:17

Code: Select all

Uri_Encode(str){
	n := StrPutVar(str, UTF8, "UTF-8"),f:={"30":1,"31":1,"32":1,"33":1,"34":1,"35":1,"36":1,"37":1,"38":1,"39":1,"41":1,"42":1,"43":1,"44":1,"45":1,"46":1,"47":1,"48":1,"49":1,"50":1,"51":1,"52":1,"53":1,"54":1,"55":1,"56":1,"57":1,"58":1,"59":1,"61":1,"62":1,"63":1,"64":1,"65":1,"66":1,"67":1,"68":1,"69":1,"70":1,"71":1,"72":1,"73":1,"74":1,"75":1,"76":1,"77":1,"78":1,"79":1,"2d":1,"2e":1,"4a":1,"4b":1,"4c":1,"4d":1,"4e":1,"4f":1,"5a":1,"5f":1,"6a":1,"6b":1,"6c":1,"6d":1,"6e":1,"6f":1,"7a":1,"7e":1}
	loop, strlen(hex:=BintoHex(&UTF8,n))/2-1
		Res .= f[r:=substr(hex, A_index*2-1,2)]?Chr("0x" r):"`%" r
	return Res
}

Uri_Decode(Uri, Enc:="UTF-8"){
	Pos := 1
	while Pos := RegExMatch(Uri, "i)(?:`%[\da-f]{2})+", Code, Pos++)
	{
		VarSetCapacity(Var, StrLen(Code.0) // 3, 0),s:=SubStr(Code.0,2)
		Loop, Parse,% s, `%
			NumPut("0x" A_LoopField, Var, A_Index - 1, "UChar")
		StrReplace, Uri, %Uri%, `%%s%, % StrGet(&Var, Enc)
	}
	Return, Uri
}
wzdg
Posts: 18
Joined: 26 Mar 2016, 22:20

Re: [H2] Uri Encode/Decode

Post by wzdg » 02 Apr 2016, 12:37

●v●没有一点文字说明,加点文字说明多好啊,方便逛论坛的,一看就知道是干什么的,怎么用了
Post Reply

Return to “脚本函数”