I've been poking around for a good, simple, fully funcitonal pair of Uri/Url/Percent encode and decode functions, and there doesn't seem to be an elegant solution. Some people say to use monstrosities of code, some people say to use javascript COM objects, and some people say to use simple solutions that don't actually work as needed.
It seems trivial to do this in other languages, so why not AHK? I'd imagine it would be placed into the Transform command, similarly to the HTML escape feature. Perhaps as "URLEncode"/"URLDecode" or "PercentEncode"/"PercentDecode".
Your thoughts on the matter?
Transform, OutputVar, URLEncode
Re: Transform, OutputVar, URLEncode
Is it trivial in "other languages" which are not aimed at web development? I can see it is sometimes useful in AutoHotkey, but not necessarily so useful as to warrant built-in functionality. All you need is a well-written script function. A built-in version would not necessarily work better.
The Transform command has been mostly superseded and has been removed from v2 (Deref has been promoted to a separate command). If any URL encoding/decoding functionality is added, it will be in the form of built-in functions.
The Transform command has been mostly superseded and has been removed from v2 (Deref has been promoted to a separate command). If any URL encoding/decoding functionality is added, it will be in the form of built-in functions.
Re: Transform, OutputVar, URLEncode
Close to half the lanuages on the rosettacode page for url encoding either have a built in function for it, or a standard library for it. That includes a few variations of BASIC, as well as C#, D, F#, Go, Java, etc.
If you don't want to implement a url encode function, perhaps you would consider a method of converting to a utf-8 bytecode instead of varsetcapacity, strput, and numget? It's kind of clunky, and confusing. I never would have thought of it if it weren't for extensive forum searching.
If you don't want to implement a url encode function, perhaps you would consider a method of converting to a utf-8 bytecode instead of varsetcapacity, strput, and numget? It's kind of clunky, and confusing. I never would have thought of it if it weren't for extensive forum searching.
Re: Transform, OutputVar, URLEncode
Okay, you've got me there. However, consider the size of those libraries compared to the size of AutoHotkey. Some of them have everything and the kitchen sink, so the fact they include URL encoding isn't saying much.
For simpler string encoding, you can copy-paste StrPutVar from the StrPut/StrGet page (or save it into your library). Since string conversion is rarely needed, I'd rather have one or two powerful built-in functions than a simple one for each task. (Rarely needed aside from the cases that are already covered: FileAppend, FileRead and FileOpen have ways of specifying the encoding, and DllCall has AStr/WStr.)
You only need NumGet to get each character because UTF-8 strings aren't readable on Unicode versions of AutoHotkey. The same would be true even if there was a simpler way to convert to UTF-8. That is, unless you're asking for a function to convert to "pseudo-UTF-8" (i.e. not UTF-8, but a similar encoding which stretches each byte out to 16 bits). That I just won't do. (Anyway, once you have pseudo-UTF-8, you'd still need to extract the code of each character, or if you're doing one character at a time, each byte/code unit.)
Again, all you need is a well-written script function. Once you have the function, you don't need to write it again or even know how it works.
For simpler string encoding, you can copy-paste StrPutVar from the StrPut/StrGet page (or save it into your library). Since string conversion is rarely needed, I'd rather have one or two powerful built-in functions than a simple one for each task. (Rarely needed aside from the cases that are already covered: FileAppend, FileRead and FileOpen have ways of specifying the encoding, and DllCall has AStr/WStr.)
You only need NumGet to get each character because UTF-8 strings aren't readable on Unicode versions of AutoHotkey. The same would be true even if there was a simpler way to convert to UTF-8. That is, unless you're asking for a function to convert to "pseudo-UTF-8" (i.e. not UTF-8, but a similar encoding which stretches each byte out to 16 bits). That I just won't do. (Anyway, once you have pseudo-UTF-8, you'd still need to extract the code of each character, or if you're doing one character at a time, each byte/code unit.)
Again, all you need is a well-written script function. Once you have the function, you don't need to write it again or even know how it works.
Re: Transform, OutputVar, URLEncode
"Other, state why in a reply"
Yes, I think a function for this is a good idea, but... All these languages are ... (if you don't mind me saying) .. "bulky/bloated" ... A library/function in "Lib\" would do fine in this case. no need to increase "bulky"-ness for everybody where only a few would use it... ... I am not necessarily disagreeing with you but.. autohotkey was not even supposed to be a "full-blown" language other then for helping simple everyday users ...
regards
Yes, I think a function for this is a good idea, but... All these languages are ... (if you don't mind me saying) .. "bulky/bloated" ... A library/function in "Lib\" would do fine in this case. no need to increase "bulky"-ness for everybody where only a few would use it... ... I am not necessarily disagreeing with you but.. autohotkey was not even supposed to be a "full-blown" language other then for helping simple everyday users ...
regards
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: Transform, OutputVar, URLEncode
Well then do we have a good standard function for this?
Recommends AHK Studio
Re: Transform, OutputVar, URLEncode
we have this for now :0
https://github.com/ahkscript/libcrypt.a ... rc/URI.ahk
https://github.com/ahkscript/libcrypt.a ... rc/URI.ahk
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: Transform, OutputVar, URLEncode
try it
%C3%84%20%C3%9C%20and%20%C3%96
%C3%84%20%C3%9C%20and%20%C3%96
Code: Select all
msgbox % LC_UriEncode("Ä Ü and Ö")
LC_UriEncode(Uri, RE="[0-9A-Za-z]") {
VarSetCapacity(Var, StrPut(Uri, "UTF-8"), 0), StrPut(Uri, &Var, "UTF-8")
While Code := NumGet(Var, A_Index - 1, "UChar")
Res .= (Chr:=Chr(Code)) ~= RE ? Chr : Format("%{:02X}", Code)
Return, Res
}
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Re: Transform, OutputVar, URLEncode
Hi, these are for UTF-8 encode/decode only, tested on 32/64bit.
Code: Select all
URI_Encode(Str, All := False)
{
Static doc := ComObjCreate("HTMLfile")
Try
{
doc.write("<body><script>document.body.innerText = encodeURI" . (All ? "Component" : "") . "(""" . Str . """);</script>")
Return, doc.body.innerText, doc.body.innerText := ""
}
}
URI_Decode(Str)
{
Static doc := ComObjCreate("HTMLfile")
Try
{
doc.write("<body><script>document.body.innerText = decodeURIComponent(""" . Str . """);</script>")
Return, doc.body.innerText, doc.body.innerText := ""
}
}
Re: Transform, OutputVar, URLEncode
That can be a bit heavy depending on what you're doing. :0
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Who is online
Users browsing this forum: No registered users and 25 guests