StringUtil: Add Ellipsise

This commit is contained in:
Stenzek 2023-09-22 20:52:35 +10:00 committed by Connor McLaughlin
parent bf9fbc46fd
commit 3c41c286c7
3 changed files with 78 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs /* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team * Copyright (C) 2002-2023 PCSX2 Dev Team
* *
* PCSX2 is free software: you can redistribute it and/or modify it under the terms * PCSX2 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 Found- * of the GNU Lesser General Public License as published by the Free Software Found-
@ -14,7 +14,10 @@
*/ */
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "Assertions.h"
#include "StringUtil.h" #include "StringUtil.h"
#include <cctype> #include <cctype>
#include <codecvt> #include <codecvt>
#include <cstdio> #include <cstdio>
@ -452,6 +455,47 @@ namespace StringUtil
return DecodeUTF8(str.data() + offset, str.length() - offset, ch); return DecodeUTF8(str.data() + offset, str.length() - offset, ch);
} }
std::string Ellipsise(const std::string_view& str, u32 max_length, const char* ellipsis /*= "..."*/)
{
std::string ret;
ret.reserve(max_length);
const u32 str_length = static_cast<u32>(str.length());
const u32 ellipsis_len = static_cast<u32>(std::strlen(ellipsis));
pxAssert(ellipsis_len > 0 && ellipsis_len <= max_length);
if (str_length > max_length)
{
const u32 copy_size = std::min(str_length, max_length - ellipsis_len);
if (copy_size > 0)
ret.append(str.data(), copy_size);
if (copy_size != str_length)
ret.append(ellipsis);
}
else
{
ret.append(str);
}
return ret;
}
void EllipsiseInPlace(std::string& str, u32 max_length, const char* ellipsis /*= "..."*/)
{
const u32 str_length = static_cast<u32>(str.length());
const u32 ellipsis_len = static_cast<u32>(std::strlen(ellipsis));
pxAssert(ellipsis_len > 0 && ellipsis_len <= max_length);
if (str_length > max_length)
{
const u32 keep_size = std::min(static_cast<u32>(str.length()), max_length - ellipsis_len);
if (keep_size != str_length)
str.erase(keep_size);
str.append(ellipsis);
}
}
#ifdef _WIN32 #ifdef _WIN32
std::wstring UTF8StringToWideString(const std::string_view& str) std::wstring UTF8StringToWideString(const std::string_view& str)
{ {

View File

@ -283,6 +283,10 @@ namespace StringUtil
size_t DecodeUTF8(const std::string_view& str, size_t offset, char32_t* ch); size_t DecodeUTF8(const std::string_view& str, size_t offset, char32_t* ch);
size_t DecodeUTF8(const std::string& str, size_t offset, char32_t* ch); size_t DecodeUTF8(const std::string& str, size_t offset, char32_t* ch);
// Replaces the end of a string with ellipsis if it exceeds the specified length.
std::string Ellipsise(const std::string_view& str, u32 max_length, const char* ellipsis = "...");
void EllipsiseInPlace(std::string& str, u32 max_length, const char* ellipsis = "...");
/// Strided memcpy/memcmp. /// Strided memcpy/memcmp.
static inline void StrideMemCpy(void* dst, std::size_t dst_stride, const void* src, std::size_t src_stride, static inline void StrideMemCpy(void* dst, std::size_t dst_stride, const void* src, std::size_t src_stride,
std::size_t copy_size, std::size_t count) std::size_t copy_size, std::size_t count)

View File

@ -113,3 +113,32 @@ TEST(StringUtil, FromCharsIsLocaleIndependent)
} }
#endif #endif
TEST(StringUtil, Ellipsise)
{
ASSERT_EQ(StringUtil::Ellipsise("HelloWorld", 6, "..."), "Hel...");
ASSERT_EQ(StringUtil::Ellipsise("HelloWorld", 7, ".."), "Hello..");
ASSERT_EQ(StringUtil::Ellipsise("HelloWorld", 20, ".."), "HelloWorld");
ASSERT_EQ(StringUtil::Ellipsise("", 20, "..."), "");
ASSERT_EQ(StringUtil::Ellipsise("Hello", 10, "..."), "Hello");
}
TEST(StringUtil, EllipsiseInPlace)
{
std::string s;
s = "HelloWorld";
StringUtil::EllipsiseInPlace(s, 6, "...");
ASSERT_EQ(s, "Hel...");
s = "HelloWorld";
StringUtil::EllipsiseInPlace(s, 7, "..");
ASSERT_EQ(s, "Hello..");
s = "HelloWorld";
StringUtil::EllipsiseInPlace(s, 20, "..");
ASSERT_EQ(s, "HelloWorld");
s = "";
StringUtil::EllipsiseInPlace(s, 20, "...");
ASSERT_EQ(s, "");
s = "Hello";
StringUtil::EllipsiseInPlace(s, 10, "...");
ASSERT_EQ(s, "Hello");
}