New function candidate for Network.URI

Graham Klyne gk at ninebynine.org
Thu Apr 7 11:28:09 EDT 2005


Using Network.URI, I find I quite often want to do relative URI 
calculations on URI strings without explicitly handling URI values.  I have 
a candidate function that might be added to Network.URI, with test cases:

[[
--  Create a new URI string that is a supplied local URI relative
--  to a given base URI, both supplied as strings.
relativeToStr :: String -> String -> Maybe String
relativeToStr rel base = do -- in Maybe monad:
     { b <- parseURIReference base
     ; r <- parseURIReference rel
     ; u <- r `relativeTo` b
     ; return $ (uriToString id u) ""
     }

test_rs1 = ("foo"       `relativeToStr ` "zz://auth/base/") == Just 
"zz://auth/base/foo"
test_rs2 = ("abs://a/u" `relativeToStr ` "zz://auth/base/") == Just "abs://a/u"
test_rs3 = ("[][]"      `relativeToStr ` "zz://auth/base/") == Nothing
test_rs4 = ("foo"       `relativeToStr ` "[][]")            == Nothing
test_rs5 = ("foo"       `relativeToStr ` "bar")             == Just "foo"
test_rs  = and [test_rs1,test_rs2,test_rs3,test_rs4,test_rs5]
]]

(I'm not sure the name is the best possible choice.)

Is this kind of helper function useful to add to Network.URI?

Maybe also a corresponding:
     relativeFromStr :: String -> String -> Maybe String
?

#g


------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Libraries mailing list