Add path utils to support StringView

This commit is contained in:
Wojtek Figat
2021-04-20 21:05:02 +02:00
parent 02856273ad
commit a7a6dc7671
4 changed files with 36 additions and 12 deletions

View File

@@ -27,6 +27,18 @@ bool StringView::operator!=(const String& other) const
return StringUtils::Compare(this->GetText(), *other) != 0;
}
String StringView::Left(int32 count) const
{
const int32 countClamped = count < 0 ? 0 : count < Length() ? count : Length();
return String(**this, countClamped);
}
String StringView::Right(int32 count) const
{
const int32 countClamped = count < 0 ? 0 : count < Length() ? count : Length();
return String(**this + Length() - countClamped);
}
String StringView::Substring(int32 startIndex) const
{
ASSERT(startIndex >= 0 && startIndex < Length());

View File

@@ -321,6 +321,20 @@ public:
public:
/// <summary>
/// Gets the left most given number of characters.
/// </summary>
/// <param name="count">The characters count.</param>
/// <returns>The substring.</returns>
String Left(int32 count) const;
/// <summary>
/// Gets the string of characters from the right (end of the string).
/// </summary>
/// <param name="count">The characters count.</param>
/// <returns>The substring.</returns>
String Right(int32 count) const;
/// <summary>
/// Retrieves substring created from characters starting from startIndex to the String end.
/// </summary>

View File

@@ -289,7 +289,7 @@ void RemoveLongPathPrefix(const String& path, String& result)
result.Remove(2, 6);
}
String StringUtils::GetDirectoryName(const String& path)
String StringUtils::GetDirectoryName(const StringView& path)
{
const int32 lastFrontSlash = path.FindLast('\\');
const int32 lastBackSlash = path.FindLast('/');
@@ -297,24 +297,22 @@ String StringUtils::GetDirectoryName(const String& path)
return splitIndex != INVALID_INDEX ? path.Left(splitIndex) : String::Empty;
}
String StringUtils::GetFileName(const String& path)
String StringUtils::GetFileName(const StringView& path)
{
Char chr;
const int32 length = path.Length();
int32 num = length;
do
{
num--;
if (num < 0)
return path;
return String(path);
chr = path[num];
} while (chr != DirectorySeparatorChar && chr != AltDirectorySeparatorChar && chr != VolumeSeparatorChar);
return path.Substring(num + 1, length - num - 1);
}
String StringUtils::GetFileNameWithoutExtension(const String& path)
String StringUtils::GetFileNameWithoutExtension(const StringView& path)
{
String filename = GetFileName(path);
const int32 num = filename.FindLast('.');
@@ -325,14 +323,14 @@ String StringUtils::GetFileNameWithoutExtension(const String& path)
return filename;
}
String StringUtils::GetPathWithoutExtension(const String& path)
String StringUtils::GetPathWithoutExtension(const StringView& path)
{
const int32 num = path.FindLast('.');
if (num != -1)
{
return path.Substring(0, num);
}
return path;
return String(path);
}
void StringUtils::PathRemoveRelativeParts(String& path)

View File

@@ -207,19 +207,19 @@ public:
// Returns the directory name of the specified path string
// @param path The path string from which to obtain the directory name
// @returns Directory name
static String GetDirectoryName(const String& path);
static String GetDirectoryName(const StringView& path);
// Returns the file name and extension of the specified path string
// @param path The path string from which to obtain the file name and extension
// @returns File name with extension
static String GetFileName(const String& path);
static String GetFileName(const StringView& path);
// Returns the file name without extension of the specified path string
// @param path The path string from which to obtain the file name
// @returns File name without extension
static String GetFileNameWithoutExtension(const String& path);
static String GetFileNameWithoutExtension(const StringView& path);
static String GetPathWithoutExtension(const String& path);
static String GetPathWithoutExtension(const StringView& path);
static void PathRemoveRelativeParts(String& path);