How to resolve the algorithm URL parser step by step in the Julia programming language
How to resolve the algorithm URL parser step by step in the Julia programming language
Table of Contents
Problem Statement
URLs are strings with a simple syntax:
Parse a well-formed URL to retrieve the relevant information: scheme, domain, path, ...
Note: this task has nothing to do with URL encoding or URL decoding.
According to the standards, the characters: only need to be percent-encoded (%) in case of possible confusion. Also note that the path, query and fragment are case sensitive, even if the scheme and domain are not. The way the returned information is provided (set of variables, array, structured, record, object,...) is language-dependent and left to the programmer, but the code should be clear enough to reuse. Extra credit is given for clear error diagnostics.
According to T. Berners-Lee foo://example.com:8042/over/there?name=ferret#nose should parse into:
urn:example:animal:ferret:nose should parse into:
other URLs that must be parsed include:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm URL parser step by step in the Julia programming language
This code snippet is written in Julia programming language and it implements a function detailview
that takes a URI
object and an optional indentlen
parameter (defaulting to 4) and returns a string with a detailed view of the URI.
The function detailview
uses a loop to iterate over the fields of the URI object and, for each field, checks if it is not empty and if it is not the port
field with value "0"
.
If both conditions are met, the function appends a string to an array s
with the format "%s%s: %s"
, where %s
is replaced by the indent
, the string representation of the field name, and the string representation of the field value, respectively.
After the loop, the function joins the strings in the array s
with a newline character ("\n"
) and returns the resulting string.
The code also includes a test
array with several URI strings, and a loop that iterates over the strings in the array and calls the detailview
function on each string, printing the result to the console.
The loop also checks if the URI is valid by calling the isvalid
function on the URI object, and prints a message accordingly.
The expected output of the code is a detailed view of each URI string in the test
array, including the scheme, authority, path, query, and fragment.
For example, for the first URI string in the test
array, the output would be:
Attempting to parse
"foo://example.com:8042/over/there?name=ferret#nose" as a URI:
This URI is parsable and appears to be valid.
scheme: foo
authority: example.com:8042
path: /over/there
query: name=ferret
fragment: nose
Source code in the julia programming language
using Printf, URIParser
const FIELDS = names(URI)
function detailview(uri::URI, indentlen::Int=4)
indent = " "^indentlen
s = String[]
for f in FIELDS
d = string(getfield(uri, f))
!isempty(d) || continue
f != :port || d != "0" || continue
push!(s, @sprintf("%s%s: %s", indent, string(f), d))
end
join(s, "\n")
end
test = ["foo://example.com:8042/over/there?name=ferret#nose",
"urn:example:animal:ferret:nose",
"jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true",
"ftp://ftp.is.co.za/rfc/rfc1808.txt",
"http://www.ietf.org/rfc/rfc2396.txt#header1",
"ldap://[2001:db8::7]/c=GB?objectClass=one&objectClass=two",
"mailto:John.Doe@example.com",
"news:comp.infosystems.www.servers.unix",
"tel:+1-816-555-1212",
"telnet://192.0.2.16:80/",
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
"This is not a URI!",
"ssh://alice@example.com",
"https://bob:pass@example.com/place",
"http://example.com/?a=1&b=2+2&c=3&c=4&d=%65%6e%63%6F%64%65%64"]
isfirst = true
for st in test
if isfirst
isfirst = false
else
println()
end
println("Attempting to parse\n \"", st, "\" as a URI:")
uri = try
URI(st)
catch
println("URIParser failed to parse this URI, is it OK?")
continue
end
print("This URI is parsable ")
if isvalid(uri)
println("and appears to be valid.")
else
println("but may be invalid.")
end
println(detailview(uri))
end
You may also check:How to resolve the algorithm Josephus problem step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the jq programming language
You may also check:How to resolve the algorithm Write entire file step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the R programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the D programming language