Yet more conversion of 'const char*' to string_view.

This commit is contained in:
Stephen Anthony 2022-12-21 20:20:30 -03:30
parent eaca65d452
commit 02d7b85fd9
5 changed files with 27 additions and 32 deletions

View File

@ -281,11 +281,8 @@ cerr << "\n\n\n";
return _realNode ? _realNode->getParent() : nullptr; return _realNode ? _realNode->getParent() : nullptr;
} }
const char* const start = _path.c_str(); cerr << "new zip: " << stemPathComponent(_path) << "\n\n\n";
const char* const end = lastPathComponent(_path); return make_unique<FSNodeZIP>(stemPathComponent(_path));
cerr << "new zip: " << string(start, end - start - 1) << "\n\n\n";
return make_unique<FSNodeZIP>(string(start, end - start - 1));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -498,24 +498,31 @@ class AbstractFSNode
*/ */
virtual size_t write(const stringstream& buffer) const { return 0; } virtual size_t write(const stringstream& buffer) const { return 0; }
protected:
/** /**
* Returns the last component of a given path. * Returns the last component of a given path.
* *
* @param str String containing the path. * @param s String containing the path.
* @return Pointer to the first char of the last component inside str. * @return View of the last component inside s.
*/ */
static const char* lastPathComponent(string_view str) static constexpr string_view lastPathComponent(string_view s)
{ {
if(str.empty()) if(s.empty()) return EmptyString;
return ""; const auto pos = s.find_last_of("/\\", s.size() - 2);
return s.substr(pos + 1);
}
const char* const start = str.data(); /**
const char* cur = start + str.size() - 2; * Returns the part *before* the last component of a given path.
*
while (cur >= start && !(*cur == '/' || *cur == '\\')) * @param s String containing the path.
--cur; * @return View of the preceding (before the last) component inside s.
*/
return cur + 1; static constexpr string_view stemPathComponent(string_view s)
{
if(s.empty()) return EmptyString;
const auto pos = s.find_last_of("/\\", s.size() - 2);
return s.substr(0, pos + 1);
} }
}; };

View File

@ -24,8 +24,8 @@
#include "LauncherFileListWidget.hxx" #include "LauncherFileListWidget.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
LauncherFileListWidget::LauncherFileListWidget(GuiObject* boss, const GUI::Font& font, LauncherFileListWidget::LauncherFileListWidget(GuiObject* boss,
int x, int y, int w, int h) const GUI::Font& font, int x, int y, int w, int h)
: FileListWidget(boss, font, x, y, w, h) : FileListWidget(boss, font, x, y, w, h)
{ {
// This widget is special, in that it catches signals and redirects them // This widget is special, in that it catches signals and redirects them

View File

@ -121,10 +121,7 @@ AbstractFSNodePtr FSNodePOSIX::getParent() const
if (_path == ROOT_DIR) if (_path == ROOT_DIR)
return nullptr; return nullptr;
const char* const start = _path.c_str(); return make_unique<FSNodePOSIX>(stemPathComponent(_path));
const char* const end = lastPathComponent(_path);
return make_unique<FSNodePOSIX>(string(start, static_cast<size_t>(end - start)));
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -102,14 +102,8 @@ AbstractFSNodePtr FSNodeWINDOWS::getParent() const
{ {
if (_isPseudoRoot) if (_isPseudoRoot)
return nullptr; return nullptr;
else if (_path.size() > 3)
if (_path.size() > 3) return make_unique<FSNodeWINDOWS>(stemPathComponent(_path));
{
const char* const start = _path.c_str();
const char* const end = lastPathComponent(_path);
return make_unique<FSNodeWINDOWS>(string(start, static_cast<size_t>(end - start)));
}
else else
return make_unique<FSNodeWINDOWS>(); return make_unique<FSNodeWINDOWS>();
} }
@ -123,11 +117,11 @@ bool FSNodeWINDOWS::getChildren(AbstractFSList& myList, ListMode mode) const
TCHAR drive_buffer[100]; TCHAR drive_buffer[100];
GetLogicalDriveStrings(sizeof(drive_buffer) / sizeof(TCHAR), drive_buffer); GetLogicalDriveStrings(sizeof(drive_buffer) / sizeof(TCHAR), drive_buffer);
char drive_name[2] = { '\0', '\0' };
for (TCHAR *current_drive = drive_buffer; *current_drive; for (TCHAR *current_drive = drive_buffer; *current_drive;
current_drive += _tcslen(current_drive) + 1) current_drive += _tcslen(current_drive) + 1)
{ {
FSNodeWINDOWS entry; FSNodeWINDOWS entry;
char drive_name[2] = { '\0', '\0' };
drive_name[0] = current_drive[0]; drive_name[0] = current_drive[0];
entry._displayName = drive_name; entry._displayName = drive_name;