Encode never gives 'a' as a short url.

Aug 15, 2010 at 1:34 AM

The Encode function has a small logic bug in it, in that it can never return an yoururl.com/a.  Now under the current structure of the solution that isn't a big deal as there is no way to get an ID of 0.  However, I've been hacking away at this and do it a bit different. 

Would recommend this new Encode function, just to help people who may implement this a tad differently.

 

            StringBuilder sb = new StringBuilder();
            int remainder;
            bool firstnum = false;
            while (number > 0 || !firstnum)
            {
                firstnum = true;
                remainder = (int)(number % URL_CHARS_LENGTH);
                number = number / URL_CHARS_LENGTH;

                sb.Append(URL_CHARS[remainder]);
            };

            return sb.ToString();

Jan 28, 2012 at 5:49 AM

Better yet, make it a do { } while() loop like so:

StringBuilder sb = new StringBuilder();
int remainder;
do
{
   remainder = (int)(number % URL_CHARS_LENGTH);
   number = number / URL_CHARS_LENGTH;
   sb.Append(URL_CHARS[remainder]);
} while (number != 0);

return sb.ToString();

Jan 28, 2012 at 11:04 PM
Honest question, what makes that better? Is there a performance improvement?


Clarence Klopfstein


On Sat, Jan 28, 2012 at 1:50 AM, IDisposable <notifications@codeplex.com> wrote:

From: IDisposable

Better yet, make it a do { } while() loop like so:

StringBuilder sb = new StringBuilder();
int remainder;
do
{
   remainder = (int)(number % URL_CHARS_LENGTH);
   number = number / URL_CHARS_LENGTH;
   sb.Append(URL_CHARS[remainder]);
} while (number != 0);

return sb.ToString();

Read the full discussion online.

To add a post to this discussion, reply to this email (jpurlshortener@discussions.codeplex.com)

To start a new discussion for this project, email jpurlshortener@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com