summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'SwifTools/Linkify.cpp')
-rw-r--r--SwifTools/Linkify.cpp53
1 files changed, 52 insertions, 1 deletions
diff --git a/SwifTools/Linkify.cpp b/SwifTools/Linkify.cpp
index 906026d..8ecbb09 100644
--- a/SwifTools/Linkify.cpp
+++ b/SwifTools/Linkify.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010 Remko Tronçon
+ * Copyright (c) 2010-2013 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
@@ -49,4 +49,55 @@ std::string Linkify::linkify(const std::string& input) {
return std::string(result.str());
}
+std::pair<std::vector<std::string>, size_t> Linkify::splitLink(const std::string& input) {
+ std::vector<std::string> result;
+ std::pair<std::vector<std::string>, size_t> pair;
+ std::vector<char> currentURL;
+ bool inURL = false;
+ size_t urlStartsAt = 0;
+ for (size_t i = 0; i < input.size(); ++i) {
+ char c = input[i];
+ if (inURL) {
+ if (c != ' ' && c != '\t' && c != '\n' && !(c == '*' && i == input.size() - 1 && input[0] == '*')) {
+ // Keep parsing
+ }
+ else {
+ std::string url(input.substr(urlStartsAt, i - urlStartsAt));
+ result.push_back(url);
+ inURL = false;
+ size_t remaining = input.size() - i;
+ if (remaining > 0) {
+ result.push_back(input.substr(i, remaining));
+ }
+ pair.first = result;
+ pair.second = urlStartsAt == 0 ? 0 : 1;
+ return pair;
+ }
+ }
+ else {
+ if (boost::regex_match(input.substr(i, 8), linkifyRegexp)) {
+ urlStartsAt = i;
+ inURL = true;
+ if (i > 0) {
+ result.push_back(input.substr(0, i));
+ }
+ }
+ else {
+ // Just keep swimming
+ }
+ }
+ }
+ if (urlStartsAt > 0 || inURL) {
+ std::string url(input.substr(urlStartsAt, input.size() - urlStartsAt));
+ result.push_back(url);
+ pair.first = result;
+ pair.second = urlStartsAt == 0 ? 0 : 1;
+ }
+ else {
+ pair.first.push_back(input);
+ pair.second = 1;
+ }
+ return pair;
+}
+
}