Skip to content

Commit

Permalink
Cleanup some string operating functions (dotnet#96099)
Browse files Browse the repository at this point in the history
* IsStringLongerThan -> strnlen

* StringCchCopy -> u16_strcpy_s

* Delete strsafe.h

* Remove FString usage in SString

* Remove fstring usage from corhlprpriv.h

* Remove fstring

* Only add link for utf8 on osx

---------
Co-authored-by: Adeel Mujahid <[email protected]>
Co-authored-by: Aaron Robinson <[email protected]>
  • Loading branch information
huoyaoyuan authored Jan 4, 2024
1 parent 6059d9f commit bc81c55
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 757 deletions.
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(errno));
}
}

// 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(errno));
}
}

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

0 comments on commit bc81c55

Please sign in to comment.