mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-06 04:57:49 +00:00
optimized parseurl module a bit
This commit is contained in:
@@ -12,59 +12,62 @@
|
||||
import strutils
|
||||
|
||||
type
|
||||
TURL* = tuple[ ## represents a *Uniform Resource Locator* (URL)
|
||||
TUrl* = tuple[ ## represents a *Uniform Resource Locator* (URL)
|
||||
## any optional component is "" if it does not exist
|
||||
scheme, username, password,
|
||||
hostname, port, path, query, anchor: string]
|
||||
|
||||
proc parseUrl*(url: string): TURL =
|
||||
var i: int = 0
|
||||
proc parseUrl*(url: string): TUrl =
|
||||
var i = 0
|
||||
|
||||
var scheme, username, password: string = ""
|
||||
var hostname, port, path, query, anchor: string = ""
|
||||
|
||||
var temp: string = ""
|
||||
var temp = ""
|
||||
|
||||
if url[i] != '/': #url isn't a relative path
|
||||
if url[i] != '/': # url isn't a relative path
|
||||
while True:
|
||||
#Scheme
|
||||
# Scheme
|
||||
if url[i] == ':':
|
||||
if url[i+1] == '/' and url[i+2] == '/':
|
||||
scheme = temp
|
||||
temp = ""
|
||||
inc(i, 3) #Skip the //
|
||||
#Authority(username, password)
|
||||
temp.setlen(0)
|
||||
inc(i, 3) # Skip the //
|
||||
# Authority(username, password)
|
||||
if url[i] == '@':
|
||||
username = temp.split(':')[0]
|
||||
if temp.split(':').len() > 1:
|
||||
password = temp.split(':')[1]
|
||||
temp = ""
|
||||
username = temp
|
||||
let colon = username.find(':')
|
||||
if colon >= 0:
|
||||
password = username.substr(colon+1)
|
||||
username = username.substr(0, colon-1)
|
||||
temp.setlen(0)
|
||||
inc(i) #Skip the @
|
||||
#hostname(subdomain, domain, port)
|
||||
# hostname(subdomain, domain, port)
|
||||
if url[i] == '/' or url[i] == '\0':
|
||||
hostname = temp
|
||||
if hostname.split(':').len() > 1:
|
||||
port = hostname.split(':')[1]
|
||||
hostname = hostname.split(':')[0]
|
||||
let colon = hostname.find(':')
|
||||
if colon >= 0:
|
||||
port = hostname.substr(colon+1)
|
||||
hostname = hostname.substr(0, colon-1)
|
||||
|
||||
temp = ""
|
||||
temp.setlen(0)
|
||||
break
|
||||
|
||||
temp.add(url[i])
|
||||
inc(i)
|
||||
|
||||
if url[i] == '/': inc(i) # Skip the '/'
|
||||
#Path
|
||||
# Path
|
||||
while True:
|
||||
if url[i] == '?':
|
||||
path = temp
|
||||
temp = ""
|
||||
temp.setlen(0)
|
||||
if url[i] == '#':
|
||||
if temp[0] == '?':
|
||||
query = temp
|
||||
else:
|
||||
path = temp
|
||||
temp = ""
|
||||
temp.setlen(0)
|
||||
|
||||
if url[i] == '\0':
|
||||
if temp[0] == '?':
|
||||
@@ -80,16 +83,25 @@ proc parseUrl*(url: string): TURL =
|
||||
|
||||
return (scheme, username, password, hostname, port, path, query, anchor)
|
||||
|
||||
proc `$`*(t: TURL): string =
|
||||
proc `$`*(u: TUrl): string =
|
||||
## turns the URL `u` into its string representation.
|
||||
result = ""
|
||||
if t.scheme != "": result.add(t.scheme & "://")
|
||||
if t.username != "":
|
||||
if t.password != "":
|
||||
result.add(t.username & ":" & t.password & "@")
|
||||
else:
|
||||
result.add(t.username & "@")
|
||||
result.add(t.hostname)
|
||||
if t.port != "": result.add(":" & t.port)
|
||||
if t.path != "": result.add("/" & t.path)
|
||||
result.add(t.query)
|
||||
result.add(t.anchor)
|
||||
if u.scheme.len > 0:
|
||||
result.add(u.scheme)
|
||||
result.add("://")
|
||||
if u.username.len > 0:
|
||||
result.add(u.username)
|
||||
if u.password.len > 0:
|
||||
result.add(":")
|
||||
result.add(u.password)
|
||||
result.add("@")
|
||||
result.add(u.hostname)
|
||||
if u.port.len > 0:
|
||||
result.add(":")
|
||||
result.add(u.port)
|
||||
if u.path.len > 0:
|
||||
result.add("/")
|
||||
result.add(u.path)
|
||||
result.add(u.query)
|
||||
result.add(u.anchor)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user