63 lines
1019 B
C++
63 lines
1019 B
C++
#include "dl_string.h"
|
|
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
|
|
namespace dl::String
|
|
{
|
|
|
|
|
|
std::string Sprintf(const char* format, ...)
|
|
{
|
|
va_list args;
|
|
int len;
|
|
|
|
va_start(args, format);
|
|
len = vsnprintf(nullptr, 0, format, args) + 1;
|
|
va_end(args);
|
|
|
|
std::string ret;
|
|
ret.resize(len);
|
|
|
|
va_start(args, format);
|
|
vsnprintf((char*)ret.data(), len, format, args);
|
|
va_end(args);
|
|
|
|
ret.pop_back();
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
std::string CvtRelativePath(std::string path_cur, std::string_view path)
|
|
{
|
|
if (!IsRelativePath(path))
|
|
return std::string{ path };
|
|
|
|
|
|
//直到首位不是 “./”和“../”
|
|
size_t i = 0;
|
|
size_t size = path.size();
|
|
while ((i < size)
|
|
&& (path[i] == '.'))
|
|
{
|
|
if ((i + 1 < size)
|
|
&& (path[i + 1] == '/'))
|
|
{//当前目录
|
|
i += 2;
|
|
}
|
|
else if ((i + 2 < size)
|
|
&& (path[i + 1] == '.' && path[i + 2] == '/'))
|
|
{//向上目录
|
|
i += 3;
|
|
path_cur.pop_back();
|
|
Erase::TailFromLast(path_cur, '/');
|
|
}
|
|
else
|
|
break;
|
|
}
|
|
return path_cur + std::string(path.begin() + i, path.end());
|
|
}
|
|
|
|
|
|
} |