From 1220463efe1a62deed197d54f3d4be5b0d282615 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 2 Jan 2018 18:59:47 -0800 Subject: [PATCH] Make GetPath less hacky for #215 --- src/language_server_api.cc | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/language_server_api.cc b/src/language_server_api.cc index 31b73e44..302194a9 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -243,35 +243,30 @@ std::string lsDocumentUri::GetPath() const { // c:/Program%20Files%20%28x86%29/Microsoft%20Visual%20Studio%2014.0/VC/include/vcruntime. // C:/Program Files (x86) - // TODO: make this not a hack. - std::string result = raw_uri; - - result = ReplaceAll(result, "%20", " "); - result = ReplaceAll(result, "%28", "("); - result = ReplaceAll(result, "%29", ")"); - - size_t index = result.find("%3A"); - if (index != std::string::npos) { - result.replace(result.begin() + index, result.begin() + index + 3, ":"); - } - - index = result.find("file://"); - if (index != std::string::npos) { -// TODO: proper fix -#if defined(_WIN32) - result.replace(result.begin() + index, result.begin() + index + 8, ""); + std::string ret; + if (raw_uri.compare(0, 8, "file:///")) + return ret; +#ifdef _WIN32 + size_t i = 8; #else - result.replace(result.begin() + index, result.begin() + index + 7, ""); + size_t i = 7; #endif + auto from_hex = [](unsigned char c) { + return c - '0' < 10 ? c - '0' : (c | 32) - 'a' + 10; + }; + for (; i < raw_uri.size(); i++) { + if (i + 3 <= raw_uri.size() && raw_uri[i] == '%') { + ret.push_back(from_hex(raw_uri[i + 1]) * 16 + from_hex(raw_uri[i + 2])); + i += 2; + } else + ret.push_back(raw_uri[i] == '\\' ? '/' : raw_uri[i]); } - std::replace(result.begin(), result.end(), '\\', '/'); - #if defined(_WIN32) // std::transform(result.begin(), result.end(), result.begin(), ::tolower); #endif - return result; + return ret; } lsPosition::lsPosition() {}