comparison Core/Toolbox.cpp @ 336:3c291753231f

url decode
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 13 Jan 2013 21:50:11 +0100
parents c9b3ba0fd140
children 26218c4de7e0
comparison
equal deleted inserted replaced
335:daff912c9ffe 336:3c291753231f
590 } 590 }
591 591
592 assert(first <= last); 592 assert(first <= last);
593 return source.substr(first, last - first); 593 return source.substr(first, last - first);
594 } 594 }
595
596
597 static uint8_t Hex2Dec(char c)
598 {
599 return ((c >= '0' && c <= '9') ? c - '0' :
600 ((c >= 'a' && c <= 'f') ? c - 'a' + 10 : c - 'A' + 10));
601 }
602
603 void Toolbox::UrlDecode(std::string& s)
604 {
605 // http://en.wikipedia.org/wiki/Percent-encoding
606 // http://www.w3schools.com/tags/ref_urlencode.asp
607 // http://stackoverflow.com/questions/154536/encode-decode-urls-in-c
608
609 if (s.size() == 0)
610 {
611 return;
612 }
613
614 size_t source = 0;
615 size_t target = 0;
616
617 while (source < s.size())
618 {
619 if (s[source] == '%' &&
620 source + 2 < s.size() &&
621 isalnum(s[source + 1]) &&
622 isalnum(s[source + 2]))
623 {
624 s[target] = (Hex2Dec(s[source + 1]) << 4) | Hex2Dec(s[source + 2]);
625 source += 3;
626 target += 1;
627 }
628 else
629 {
630 if (s[source] == '+')
631 s[target] = ' ';
632 else
633 s[target] = s[source];
634
635 source++;
636 target++;
637 }
638 }
639
640 s.resize(target);
641 }
595 } 642 }