Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup some string operating functions #96099

Merged
merged 16 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/coreclr/debug/daccess/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,8 @@ ClrDataValue::GetString(
{
*strLen = static_cast<ULONG32>(u16_strlen(msgStr) + 1);
}
status = StringCchCopy(str, bufLen, msgStr) == S_OK ?
S_OK : S_FALSE;

status = u16_strcpy_s(str, bufLen, msgStr) != NULL ? S_OK : S_FALSE;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/daccess/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ ClrDataFrame::GetArgumentByIndex(
*nameLen = 5;
}

StringCchCopy(name, bufLen, W("this"));
u16_strcpy_s(name, bufLen, W("this"));
}
else
{
Expand Down
5 changes: 0 additions & 5 deletions src/coreclr/debug/daccess/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@
#include "dacimpl.h"


#define STRSAFE_NO_DEPRECATE
#include <strsafe.h>
#undef _ftcscat
#undef _ftcscpy

// from ntstatus.h
#define STATUS_STOWED_EXCEPTION ((NTSTATUS)0xC000027BL)

Expand Down
5 changes: 2 additions & 3 deletions src/coreclr/debug/daccess/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,7 @@ ClrDataAppDomain::GetName(
}
else
{
status = StringCchCopy(name, bufLen, (PCWSTR)rawName) == S_OK ?
S_OK : S_FALSE;
status = u16_strcpy_s(name, bufLen, (PCWSTR)rawName) != NULL ? S_OK : S_FALSE;
if (nameLen)
{
size_t cchName = u16_strlen((PCWSTR)rawName) + 1;
Expand Down Expand Up @@ -4768,7 +4767,7 @@ ClrDataExceptionState::GetString(
message->GetStringLength(),
true);

status = StringCchCopy(str, bufLen, msgStr) == S_OK ? S_OK : S_FALSE;
status = u16_strcpy_s(str, bufLen, msgStr) != NULL ? S_OK : S_FALSE;
if (strLen != NULL)
{
size_t cchName = u16_strlen(msgStr) + 1;
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/ilasm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "clrversion.h"
#include "shimload.h"

#include "strsafe.h"
#define ASSERTE_ALL_BUILDS(expr) _ASSERTE_ALL_BUILDS((expr))

WCHAR* EqualOrColon(_In_ __nullterminated WCHAR* szArg)
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/inc/clr/fs/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#include "clrtypes.h"

#include "strsafe.h"

#include "clr/str.h"

namespace clr
Expand Down
64 changes: 13 additions & 51 deletions src/coreclr/inc/corhlprpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define __CORHLPRPRIV_H__

#include "corhlpr.h"
#include "fstring.h"
#include <minipal/utf8.h>

#if defined(_MSC_VER) && defined(HOST_X86)
#pragma optimize("y", on) // If routines don't get inlined, don't pay the EBP frame penalty
Expand Down Expand Up @@ -225,71 +225,33 @@ class CQuickMemoryBase
iSize = cbTotal;
}


// Convert UTF8 string to UNICODE string, optimized for speed
HRESULT ConvertUtf8_UnicodeNoThrow(const char * utf8str)
{
bool allAscii;
DWORD length;

HRESULT hr = FString::Utf8_Unicode_Length(utf8str, & allAscii, & length);

if (SUCCEEDED(hr))
{
LPWSTR buffer = (LPWSTR) AllocNoThrow((length + 1) * sizeof(WCHAR));

if (buffer == NULL)
{
hr = E_OUTOFMEMORY;
}
else
{
hr = FString::Utf8_Unicode(utf8str, allAscii, buffer, length);
}
}

return hr;
}

// Convert UTF8 string to UNICODE string, optimized for speed
void ConvertUtf8_Unicode(const char * utf8str)
{
bool allAscii;
DWORD length;
size_t sourceLen = strlen(utf8str);
size_t destLen = minipal_get_length_utf8_to_utf16(utf8str, sourceLen, 0);

HRESULT hr = FString::Utf8_Unicode_Length(utf8str, & allAscii, & length);
CHAR16_T* buffer = (CHAR16_T*) AllocThrows((destLen + 1) * sizeof(CHAR16_T));
buffer[destLen] = W('\0');

if (SUCCEEDED(hr))
if (!minipal_convert_utf8_to_utf16(utf8str, sourceLen, buffer, destLen, 0))
{
LPWSTR buffer = (LPWSTR) AllocThrows((length + 1) * sizeof(WCHAR));

hr = FString::Utf8_Unicode(utf8str, allAscii, buffer, length);
}

if (FAILED(hr))
{
ThrowHR(hr);
ThrowHR(EMAKEHR(ERROR_NO_UNICODE_TRANSLATION));
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved
}
}

// Convert UNICODE string to UTF8 string, optimized for speed
void ConvertUnicode_Utf8(const WCHAR * pString)
{
bool allAscii;
DWORD length;

HRESULT hr = FString::Unicode_Utf8_Length(pString, & allAscii, & length);
size_t sourceLen = u16_strlen(pString);
size_t destLen = minipal_get_length_utf16_to_utf8((const CHAR16_T*)pString, sourceLen, 0);

if (SUCCEEDED(hr))
{
LPSTR buffer = (LPSTR) AllocThrows((length + 1) * sizeof(char));

hr = FString::Unicode_Utf8(pString, allAscii, buffer, length);
}
LPSTR buffer = (LPSTR) AllocThrows((destLen + 1) * sizeof(char));
buffer[destLen] = '\0';

if (FAILED(hr))
if (!minipal_convert_utf16_to_utf8((const CHAR16_T*)pString, sourceLen, buffer, destLen, 0))
{
ThrowHR(hr);
ThrowHR(EMAKEHR(ERROR_NO_UNICODE_TRANSLATION));
huoyaoyuan marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
44 changes: 0 additions & 44 deletions src/coreclr/inc/fstring.h

This file was deleted.

21 changes: 0 additions & 21 deletions src/coreclr/inc/utilcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2834,27 +2834,6 @@ template <class T> class CChainedHash
};


//*****************************************************************************
//
//********** String helper functions.
//
//*****************************************************************************

//*****************************************************************************
// Checks if string length exceeds the specified limit
//*****************************************************************************
inline BOOL IsStrLongerThan(_In_ _In_z_ char* pstr, unsigned N)
{
LIMITED_METHOD_CONTRACT;
unsigned i = 0;
if(pstr)
{
for(i=0; (i < N)&&(pstr[i]); i++);
}
return (i >= N);
}


//*****************************************************************************
// Class to parse a list of simple assembly names and then find a match
//*****************************************************************************
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/minipal/Unix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ if(NOT CLR_CROSS_COMPONENTS_BUILD)
list(APPEND SOURCES
${CLR_SRC_NATIVE_DIR}/minipal/cpufeatures.c
)

if(CLR_CMAKE_HOST_OSX)
list(APPEND SOURCES
${CLR_SRC_NATIVE_DIR}/minipal/utf8.c
)
endif()
endif()

add_library(coreclrminipal
Expand Down
Loading