Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparing URLS

 
Mark Fletcher
Ranch Hand
Posts: 897
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you have a page with the url

http://foo/bar/bar1/bar2/page.htm

and it has a link

http://foo/bar3/bar4/page2.htm

Suggest an method of producing a relative URL to url1 for url2

ie

../../../bar3/bar4/page2.htm
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Fletcher:
Say you have a page with the url

http://foo/bar/bar1/bar2/page.htm

and it has a link

http://foo/bar3/bar4/page2.htm

Suggest an method of producing a relative URL to url1 for url2

ie

../../../bar3/bar4/page2.htm



i have thought about it, but doesnt have time yet to code and test my pseudocode:

given: url1(the one containing the link to url2), url2

1. count tokens(folders) for url1
e.g. for "http://foo/bar3/bar4/page2.htm", there are 3 tokens, foo, bar3 and bar4

2.
dots="";
extract the filename and put it to webPageFilename(e.g. page2.htm)

for (i=tokens;i>0;i--)
{
get substring of url1 from start of url1 up to token i;
get substring of url2 from start of url2 up to token i;
if both substring are same
{
return dots+remaining tokens for url2+webPageFilename;
}
dots=dots+'../';
}
return 'must use absolute url';
}
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the two into a stack (reverse the second), pop from the first and append "../" until it exists in the second stack, pop from the second stack till you get the common element, then start appending till you run out.

This assumes the lengths are small. If the common parts are much larger than the separate bits it may be faster not to reverse the second, pop off the second and prepend them to a separate string then join the two Strings together.

Then again you can start by popping and prepending from the second till you reach the common element, then pop from the first and prepend the "../" till you again reach the common element. This is a little more direct but probably harder to read after the fact.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not correct, I'll leave the reason why as an excersize for readers. I'll update when I get a moment
 
Sania Marsh
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm bit confused about second solution,
(are we putting first element at the bottom of the stack or last... but we are still looking for same element, when we shoould reather look for different one)
but I think we cannot compare from end. foo may have foo within itself. or 3 levels down. You are never guranteed to have different folder names.

We should rather go from front, until hit different elelments, then count the remaining "tokens" and prepend them as dots.

Very interesting. I'll try to write it once get time
[ August 20, 2005: Message edited by: Sania Marsh ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
foo may have foo within itself.
Yep, this is the problem I was referring to.
You are quite correct.
 
Nimai Etheridge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I split the strings into array's and then compared each element of the array. I guess it's the same as tokenizing the string



the relativeDir boolean ensures you don't get output like
../bar/../foo.html

it's not the funkiest code, but it works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic