common: Add string utilities for dealing with entry point conventions.

Used across both vkd3d-shader and vkd3d, so makes sense to move this to
common code.

Signed-off-by: Hans-Kristian Arntzen <post@arntzen-software.no>
This commit is contained in:
Hans-Kristian Arntzen 2021-01-28 10:53:55 +01:00
parent 4957d561dc
commit 86da5d9bad
3 changed files with 145 additions and 1 deletions

View File

@ -0,0 +1,37 @@
/*
* Copyright 2021 Hans-Kristian Arntzen for Valve Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef __VKD3D_STRING_H
#define __VKD3D_STRING_H
#include "vkd3d_common.h"
#include <stddef.h>
/* Various string utilities. */
WCHAR *vkd3d_dup_entry_point(const char *str);
WCHAR *vkd3d_dup_entry_point_n(const char *str, size_t len);
WCHAR *vkd3d_dup_demangled_entry_point(const char *str);
bool vkd3d_export_strequal(const WCHAR *a, const WCHAR *b);
char *vkd3d_strdup(const char *str);
WCHAR *vkd3d_wstrdup(const WCHAR *str);
WCHAR *vkd3d_wstrdup_n(const WCHAR *str, size_t n);
#endif /* __VKD3D_STRING_H */

View File

@ -2,7 +2,8 @@ vkd3d_common_src = [
'debug.c',
'memory.c',
'utf8.c',
'profiling.c'
'profiling.c',
'string.c',
]
vkd3d_common_lib = static_library('vkd3d_common', vkd3d_common_src, vkd3d_header_files,

106
libs/vkd3d-common/string.c Normal file
View File

@ -0,0 +1,106 @@
/*
* Copyright 2021 Hans-Kristian Arntzen for Valve Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#define VKD3D_DBG_CHANNEL VKD3D_DBG_CHANNEL_API
#include "vkd3d_string.h"
#include "vkd3d_memory.h"
char *vkd3d_strdup(const char *str)
{
/* strdup() is actually not standard. */
char *duped;
size_t len;
len = strlen(str) + 1;
duped = vkd3d_malloc(len);
if (duped)
memcpy(duped, str, len);
return duped;
}
WCHAR *vkd3d_wstrdup(const WCHAR *str)
{
WCHAR *duped;
size_t len;
len = vkd3d_wcslen(str, sizeof(WCHAR)) + 1;
duped = vkd3d_malloc(len * sizeof(WCHAR));
if (duped)
memcpy(duped, str, len * sizeof(WCHAR));
return duped;
}
bool vkd3d_export_strequal(const WCHAR *a, const WCHAR *b)
{
if (!a || !b)
return false;
while (*a != '\0' && *b != '\0')
{
if (*a != *b)
return false;
a++;
b++;
}
return *a == *b;
}
WCHAR *vkd3d_dup_entry_point(const char *str)
{
return vkd3d_dup_entry_point_n(str, strlen(str));
}
WCHAR *vkd3d_dup_entry_point_n(const char *str, size_t len)
{
WCHAR *duped;
size_t i;
duped = vkd3d_malloc((len + 1) * sizeof(WCHAR));
if (!duped)
return NULL;
for (i = 0; i < len; i++)
duped[i] = (unsigned char)str[i];
duped[len] = 0;
return duped;
}
static bool is_valid_identifier_character(char v)
{
return (v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || v == '_';
}
WCHAR *vkd3d_dup_demangled_entry_point(const char *entry)
{
const char *end_entry;
while (*entry != '\0' && !is_valid_identifier_character(*entry))
entry++;
end_entry = entry;
while (*end_entry != '\0' && is_valid_identifier_character(*end_entry))
end_entry++;
if (entry == end_entry)
return NULL;
return vkd3d_dup_entry_point_n(entry, end_entry - entry);
}