Move copyToChar32 to the one file where it's used

This commit is contained in:
Chris Thrasher committed 2024-09-05 13:41:26 -06:00
1 parent d50ff75b44
commit 34f95771d8
2 files changed
+26 -60

No files matched your search

-57
View File
@@ -1,57 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <CSFML/Config.h>
#include <SFML/System/String.hpp>
#include <cstring>
////////////////////////////////////////////////////////////
// Ensure char32_t has the same size as sfChar32 (uint32_t)
// Identical alignment and size is required because we're type punning
// when doing sfChar32* <-> char32_t* casts
////////////////////////////////////////////////////////////
static_assert(sizeof(sfChar32) == sizeof(char32_t));
static_assert(alignof(sfChar32) == alignof(char32_t));
////////////////////////////////////////////////////////////
// Define utils to copy from / to sfChar32 and sf::String
////////////////////////////////////////////////////////////
[[nodiscard]] inline sfChar32* copyToChar32(const sf::String& str)
{
std::size_t byteCount = sizeof(sfChar32) * str.getSize();
auto* utf32 = static_cast<sfChar32*>(malloc(byteCount + sizeof(sfChar32)));
memcpy(utf32, str.getData(), byteCount);
utf32[str.getSize()] = 0;
return utf32;
}
+26 -3
View File
@@ -25,7 +25,6 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <CSFML/Char32.hpp>
#include <CSFML/Network/Ftp.h>
#include <CSFML/Network/FtpStruct.hpp>
@@ -35,6 +34,31 @@
#include <cstring>
namespace
{
////////////////////////////////////////////////////////////
// Ensure char32_t has the same size as sfChar32 (uint32_t)
// Identical alignment and size is required because we're type punning
// when doing sfChar32* <-> char32_t* casts
////////////////////////////////////////////////////////////
static_assert(sizeof(sfChar32) == sizeof(char32_t));
static_assert(alignof(sfChar32) == alignof(char32_t));
////////////////////////////////////////////////////////////
// Define utils to copy to sfChar32
////////////////////////////////////////////////////////////
[[nodiscard]] sfChar32* copyToChar32(const sf::String& str)
{
const std::size_t byteCount = sizeof(sfChar32) * str.getSize();
auto* utf32 = static_cast<sfChar32*>(std::malloc(byteCount + sizeof(sfChar32)));
std::memcpy(utf32, str.getData(), byteCount);
utf32[str.getSize()] = 0;
return utf32;
}
} // namespace
////////////////////////////////////////////////////////////
void sfFtpListingResponse_destroy(sfFtpListingResponse* ftpListingResponse)
{
@@ -125,8 +149,7 @@ const char* sfFtpDirectoryResponse_getDirectory(const sfFtpDirectoryResponse* ft
const sfChar32* sfFtpDirectoryResponse_getDirectoryUnicode(const sfFtpDirectoryResponse* ftpDirectoryResponse)
{
assert(ftpDirectoryResponse);
sf::String str = sf::String(ftpDirectoryResponse->This.getDirectory().c_str());
return copyToChar32(str);
return copyToChar32(sf::String(ftpDirectoryResponse->This.getDirectory().string()));
}