|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | #include <srcml_input_src.hpp> |
| 11 | +#include <input_curl.hpp> |
11 | 12 | #include <string_view> |
12 | 13 |
|
13 | 14 | using namespace ::std::literals::string_view_literals; |
@@ -55,6 +56,129 @@ srcml_input_src::srcml_input_src(std::string_view other) { |
55 | 56 | resource = resource.substr(0, query_pos); |
56 | 57 | } |
57 | 58 | } |
| 59 | + |
| 60 | + // local files, and urls, may carry a trailing range suffix, e.g. "main.cpp:191", "main.cpp:191:5", or "main.cpp:191:5-222:12" |
| 61 | + if (protocol == "file"sv || curl_supported(protocol)) { |
| 62 | + |
| 63 | + // a number at position i of s, advancing i, where a position of zero is invalid, |
| 64 | + // and a number too large for a position is not part of a suffix at all |
| 65 | + auto number = [](std::string_view s, size_t& i) -> std::optional<int> { |
| 66 | + |
| 67 | + size_t digits = i; |
| 68 | + while (digits < s.size() && s[digits] >= '0' && s[digits] <= '9') |
| 69 | + ++digits; |
| 70 | + |
| 71 | + if (digits == i) |
| 72 | + return std::nullopt; |
| 73 | + |
| 74 | + int position = 0; |
| 75 | + try { |
| 76 | + position = std::stoi(std::string(s.substr(i, digits - i))); |
| 77 | + } catch (const std::out_of_range&) { |
| 78 | + return std::nullopt; |
| 79 | + } |
| 80 | + |
| 81 | + i = digits; |
| 82 | + |
| 83 | + return position != 0 ? position : INVALID_POSITION; |
| 84 | + }; |
| 85 | + |
| 86 | + // "LINE[:COLUMN][-[LINE[:COLUMN]]]" or "-LINE[:COLUMN]", the part of a suffix after the colon |
| 87 | + auto parse_suffix = [&number](std::string_view s, int& line, int& column, int& end_line, int& end_column) { |
| 88 | + |
| 89 | + line = 1; |
| 90 | + column = 0; |
| 91 | + end_line = 0; |
| 92 | + end_column = 0; |
| 93 | + |
| 94 | + size_t i = 0; |
| 95 | + bool isstart = !s.empty() && s[0] != '-'; |
| 96 | + |
| 97 | + if (isstart) { |
| 98 | + |
| 99 | + auto n = number(s, i); |
| 100 | + if (!n) |
| 101 | + return false; |
| 102 | + |
| 103 | + line = *n; |
| 104 | + |
| 105 | + if (i < s.size() && s[i] == ':') { |
| 106 | + |
| 107 | + auto c = number(s, ++i); |
| 108 | + if (!c) |
| 109 | + return false; |
| 110 | + |
| 111 | + column = *c; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // without a range, the end is the end of the start line |
| 116 | + if (i == s.size()) { |
| 117 | + |
| 118 | + end_line = line; |
| 119 | + |
| 120 | + return isstart; |
| 121 | + } |
| 122 | + |
| 123 | + if (s[i] != '-') |
| 124 | + return false; |
| 125 | + |
| 126 | + // an open range ends at the end of the file |
| 127 | + if (++i == s.size()) { |
| 128 | + |
| 129 | + end_line = END_OF_FILE; |
| 130 | + |
| 131 | + return isstart; |
| 132 | + } |
| 133 | + |
| 134 | + auto n = number(s, i); |
| 135 | + if (!n) |
| 136 | + return false; |
| 137 | + |
| 138 | + end_line = *n; |
| 139 | + |
| 140 | + if (i < s.size() && s[i] == ':') { |
| 141 | + |
| 142 | + auto c = number(s, ++i); |
| 143 | + if (!c) |
| 144 | + return false; |
| 145 | + |
| 146 | + end_column = *c; |
| 147 | + } |
| 148 | + |
| 149 | + return i == s.size(); |
| 150 | + }; |
| 151 | + |
| 152 | + // the suffix starts at the first colon of the filename whose remainder is an entire suffix |
| 153 | + size_t base = resource.find_last_of("/\\"); |
| 154 | + for (size_t colon_pos = resource.find(':', base == std::string::npos ? 0 : base + 1); |
| 155 | + colon_pos != std::string::npos; colon_pos = resource.find(':', colon_pos + 1)) { |
| 156 | + |
| 157 | + int suffix_line = 0; |
| 158 | + int suffix_column = 0; |
| 159 | + int suffix_end_line = 0; |
| 160 | + int suffix_end_column = 0; |
| 161 | + |
| 162 | + if (!parse_suffix(std::string_view(resource).substr(colon_pos + 1), suffix_line, suffix_column, suffix_end_line, suffix_end_column)) |
| 163 | + continue; |
| 164 | + |
| 165 | + // a file of that exact name, suffix and all, is a filename, not a suffix |
| 166 | + struct stat s; |
| 167 | + if (protocol != "file"sv || stat(resource.data(), &s) != 0) { |
| 168 | + |
| 169 | + line = suffix_line; |
| 170 | + column = suffix_column; |
| 171 | + end_line = suffix_end_line; |
| 172 | + end_column = suffix_end_column; |
| 173 | + |
| 174 | + resource = resource.substr(0, colon_pos); |
| 175 | + filename = src_prefix_add_uri(protocol, resource); |
| 176 | + } |
| 177 | + |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + |
58 | 182 | exists = false; |
59 | 183 |
|
60 | 184 | if (protocol == "file"sv) { |
|
0 commit comments