How to resolve the algorithm URL encoding step by step in the M2000 Interpreter programming language
How to resolve the algorithm URL encoding step by step in the M2000 Interpreter programming language
Table of Contents
Problem Statement
Provide a function or mechanism to convert a provided string into URL encoding representation. In URL encoding, special characters, control characters and extended characters are converted into a percent symbol followed by a two digit hexadecimal code, So a space character encodes into %20 within the string. For the purposes of this task, every character except 0-9, A-Z and a-z requires conversion, so the following characters all require conversion by default:
The string "http://foo bar/" would be encoded as "http%3A%2F%2Ffoo%20bar%2F".
It is permissible to use an exception string (containing a set of symbols that do not need to be converted). However, this is an optional feature and is not a requirement of this task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm URL encoding step by step in the M2000 Interpreter programming language
Source code in the m2000 programming language
Module Checkit {
Function decodeUrl$(a$) {
DIM a$()
a$()=Piece$(a$, "%")
if len(a$())=1 then =str$(a$):exit
k=each(a$(),2)
acc$=str$(a$(0))
While k {
acc$+=str$(Chr$(Eval("0x"+left$(a$(k^),2)))+Mid$(a$(k^),3))
}
=string$(acc$ as utf8dec)
}
Group Parse$ {
all$, c=1
tc$=""
Enum UrlType {None=0, RFC3986, HTML5}
variation
TypeData=("","-._~","-._*")
Function Next {
.tc$<=mid$(.all$,.c,1)
.c++
=.tc$<>""
}
Value {
=.tc$
}
Function DecodeOne$ {
if .tc$="" then exit
if .tc$ ~"[A-Za-z0-9]" then =.tc$ : exit
If .tc$=" " Then =if$(.variation=.HTML5->"+","%20") :exit
if instr(.TypeData#val$(.variation),.tc$)>0 then =.tc$ :exit
="%"+hex$(asc(.tc$), 1)
}
Function Decode$ {
acc$=""
.c<=1
While .Next() {
acc$+=.DecodeOne$()
}
=acc$
}
Set () {
\\ using optional argument
var=.None
Read a$, ? var
a$=chr$(string$(a$ as utf8enc))
.variation<=var
.all$<=a$
.c<=1
}
}
\\ MAIN
Parse$()="http://foo bar/"
Print Quote$(Parse.Decode$())
Parse.variation=Parse.HTML5
Print Quote$(Parse.Decode$())
Parse.variation=Parse.RFC3986
Print Quote$(Parse.Decode$())
Parse$(Parse.RFC3986) ={mailto:"Irma User" }
Print Quote$(Parse.Decode$())
Parse$(Parse.RFC3986) ={http://foo.bar.com/~user-name/_subdir/*~.html}
m=each(Parse.UrlType)
while m {
Parse.variation=eval(m)
Print Quote$(Parse.Decode$())
Print decodeUrl$(Parse.Decode$())
}
}
CheckIt
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the Perl programming language
You may also check:How to resolve the algorithm Top rank per group step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Percentage difference between images step by step in the Haskell programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Burlesque programming language