Cogs.Core
EmscriptenUtilities.cpp
1#include "EmscriptenUtilities.h"
2
3std::string Cogs::Core::EmscriptenUtilities::combine(const std::string & a, const std::string & b)
4{
5 if (a.empty()) return b;
6
7 size_t contentA = a.length();
8 while (a.at(contentA -1) == '/' && contentA>0)
9 {
10 contentA--;
11 }
12
13 size_t offsetB = 0;
14 while (b.at(offsetB) == '/' && offsetB <b.length())
15 {
16 offsetB++;
17 }
18
19 // For some urls with parameter list where we dont want / between search path and resource name. Eg. service?fileName=resourceName
20 auto combined = a.back() == '=' ? a + b : a.substr(0, contentA) + "/" + b.substr(offsetB, b.length()-offsetB);
21 if (combined.find("://") == std::string::npos)
22 {
23 replaceAll(combined, "//", "/");
24 }
25
26 replaceAll(combined, "\\", "/");
27 replaceAll(combined, "\\\\", "/");
28
29 return combined;
30}
31
32void Cogs::Core::EmscriptenUtilities::replaceAll(std::string & str, const std::string & from, const std::string & to)
33{
34 if (from.empty()) return;
35
36 size_t startPos = 0;
37 while ((startPos = str.find(from, startPos)) != std::string::npos) {
38 str.replace(startPos, from.length(), to);
39 startPos += to.length();
40 }
41}