Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save BillyONeal/eede19d643e62d0eeec814ebdd5ff658 to your computer and use it in GitHub Desktop.

Select an option

Save BillyONeal/eede19d643e62d0eeec814ebdd5ff658 to your computer and use it in GitHub Desktop.
_NODISCARD inline unique_ptr<wchar_t[]> _Get_cleaned_symlink_target(const path& _To) noexcept
{ // transforms /s in the root-name to \s, and all other directory-separators into single \s
// example: a/\/b -> a\b
// example: //server/a////////b////////c////////d -> \\server\a\b\c\d
const auto& _To_str = _To.native();
// protected from overflow by wstring's max_size cap:
unique_ptr<wchar_t[]> _Cleaned_link(::new (nothrow) wchar_t[_To_str.size() + 1]);
if (!_Cleaned_link)
{
return (_Cleaned_link);
}
const auto _First = _To_str.c_str();
const auto _Last = _First + _To_str.size();
auto _Next = _Find_root_name_end(_First, _Last);
auto _Dest = _STD replace_copy_if(_First, _Next, _Cleaned_link.get(), _Is_slash, L'\\');
for (;;)
{
const wchar_t _Ch = *_Next;
if (_Is_slash(_Ch))
{
*_Dest = L'\\';
do
{
++_Next;
}
while (_Is_slash(*_Next));
}
else
{
*_Dest = _Ch;
if (_Ch == L'\0')
{
break;
}
++_Next;
}
++_Dest;
}
return (_Cleaned_link);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment