From 3737b424d6c8ef4fb3ea393e9407361a7e095d94 Mon Sep 17 00:00:00 2001 From: npwoods Date: Wed, 14 Sep 2022 13:02:30 -0400 Subject: util/corestr.cpp: Changed core_stricmp to take std::string_view parameters. (#10287) Note that the implementation is still not UTF-8 aware. --- src/frontend/mame/ui/custui.cpp | 12 ++++++------ src/frontend/mame/ui/filesel.cpp | 2 +- src/frontend/mame/ui/inifile.cpp | 2 +- src/frontend/mame/ui/menu.cpp | 6 +++--- src/frontend/mame/ui/selmenu.cpp | 2 +- src/lib/util/corestr.cpp | 20 ++++++++++++++------ src/lib/util/corestr.h | 2 +- src/osd/modules/font/font_sdl.cpp | 4 ++-- src/osd/modules/font/font_windows.cpp | 2 +- src/tools/imgtool/library.cpp | 2 +- src/tools/imgtool/modules/amiga.cpp | 22 ++++++++++++++-------- src/tools/imgtool/modules/dgndos.cpp | 2 +- src/tools/imgtool/modules/rsdos.cpp | 2 +- 13 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/frontend/mame/ui/custui.cpp b/src/frontend/mame/ui/custui.cpp index 08cad10ccdd..090f657169a 100644 --- a/src/frontend/mame/ui/custui.cpp +++ b/src/frontend/mame/ui/custui.cpp @@ -250,7 +250,7 @@ void menu_custom_ui::find_languages() std::sort( std::next(m_languages.begin()), m_languages.end(), - [] (std::string const &x, std::string const &y) { return 0 > core_stricmp(x.c_str(), y.c_str()); }); + [] (std::string const &x, std::string const &y) { return 0 > core_stricmp(x, y); }); char const *const lang = machine().options().language(); if (*lang) @@ -259,8 +259,8 @@ void menu_custom_ui::find_languages() std::next(m_languages.begin()), m_languages.end(), lang, - [] (std::string const &x, char const *y) { return 0 > core_stricmp(x.c_str(), y); }); - if ((m_languages.end() != found) && !core_stricmp(found->c_str(), lang)) + [] (std::string const &x, char const *y) { return 0 > core_stricmp(x, y); }); + if ((m_languages.end() != found) && !core_stricmp(*found, lang)) m_currlang = std::distance(m_languages.begin(), found); } else @@ -292,7 +292,7 @@ void menu_custom_ui::find_sysnames() std::sort( m_sysnames.begin(), m_sysnames.end(), - [] (std::string const &x, std::string const &y) { return 0 > core_stricmp(x.c_str(), y.c_str()); }); + [] (std::string const &x, std::string const &y) { return 0 > core_stricmp(x, y); }); char const *const names = ui().options().system_names(); if (*names) @@ -301,9 +301,9 @@ void menu_custom_ui::find_sysnames() std::next(m_sysnames.begin()), m_sysnames.end(), names, - [] (std::string const &x, char const *y) { return 0 > core_stricmp(x.c_str(), y); }); + [] (std::string const &x, char const *y) { return 0 > core_stricmp(x, y); }); m_currsysnames = std::distance(m_sysnames.begin(), found); - if ((m_sysnames.end() == found) || core_stricmp(found->c_str(), names)) + if ((m_sysnames.end() == found) || core_stricmp(*found, names)) m_sysnames.emplace(found, names); } else diff --git a/src/frontend/mame/ui/filesel.cpp b/src/frontend/mame/ui/filesel.cpp index 704aaba3686..664bd699c47 100644 --- a/src/frontend/mame/ui/filesel.cpp +++ b/src/frontend/mame/ui/filesel.cpp @@ -397,7 +397,7 @@ void menu_file_selector::populate(float &customtop, float &custombottom) selected_entry = entry; // do we have to select this file? - if (!core_stricmp(m_current_file.c_str(), dirent->name)) + if (!core_stricmp(m_current_file, dirent->name)) selected_entry = entry; } } diff --git a/src/frontend/mame/ui/inifile.cpp b/src/frontend/mame/ui/inifile.cpp index 4475210db74..55c7da2d919 100644 --- a/src/frontend/mame/ui/inifile.cpp +++ b/src/frontend/mame/ui/inifile.cpp @@ -532,7 +532,7 @@ void favorite_manager::update_sorted() int cmp; - cmp = core_stricmp(lhs.longname.c_str(), rhs.longname.c_str()); + cmp = core_stricmp(lhs.longname, rhs.longname); if (0 > cmp) return true; else if (0 < cmp) diff --git a/src/frontend/mame/ui/menu.cpp b/src/frontend/mame/ui/menu.cpp index dbe1b29a813..0ad1ad0b379 100644 --- a/src/frontend/mame/ui/menu.cpp +++ b/src/frontend/mame/ui/menu.cpp @@ -719,13 +719,13 @@ void menu::draw(uint32_t flags) } // customize subitem text color - if (!core_stricmp(pitem.subtext().c_str(), _("On"))) + if (!core_stricmp(pitem.subtext(), _("On"))) fgcolor2 = rgb_t(0x00,0xff,0x00); - if (!core_stricmp(pitem.subtext().c_str(), _("Off"))) + if (!core_stricmp(pitem.subtext(), _("Off"))) fgcolor2 = rgb_t(0xff,0x00,0x00); - if (!core_stricmp(pitem.subtext().c_str(), _("Auto"))) + if (!core_stricmp(pitem.subtext(), _("Auto"))) fgcolor2 = rgb_t(0xff,0xff,0x00); // draw the subitem right-justified diff --git a/src/frontend/mame/ui/selmenu.cpp b/src/frontend/mame/ui/selmenu.cpp index 9b1b3f1e74d..a37b7215918 100644 --- a/src/frontend/mame/ui/selmenu.cpp +++ b/src/frontend/mame/ui/selmenu.cpp @@ -278,7 +278,7 @@ void menu_select_launch::software_parts::populate(float &customtop, float &custo parts.reserve(m_parts.size()); for (s_parts::const_iterator it = m_parts.begin(); m_parts.end() != it; ++it) parts.push_back(it); - std::sort(parts.begin(), parts.end(), [] (auto const &left, auto const &right) { return 0 > core_stricmp(left->first.c_str(), right->first.c_str()); }); + std::sort(parts.begin(), parts.end(), [] (auto const &left, auto const &right) { return 0 > core_stricmp(left->first, right->first); }); for (auto const &elem : parts) item_append(elem->first, elem->second, 0, (void *)&*elem); diff --git a/src/lib/util/corestr.cpp b/src/lib/util/corestr.cpp index 50f2469b14f..1d34e2258a8 100644 --- a/src/lib/util/corestr.cpp +++ b/src/lib/util/corestr.cpp @@ -22,14 +22,22 @@ core_stricmp - case-insensitive string compare -------------------------------------------------*/ -int core_stricmp(const char *s1, const char *s2) +int core_stricmp(std::string_view s1, std::string_view s2) { - for (;;) + auto s1_iter = s1.begin(); + auto s2_iter = s2.begin(); + while (true) { - int c1 = tolower((uint8_t)*s1++); - int c2 = tolower((uint8_t)*s2++); - if (c1 == 0 || c1 != c2) - return c1 - c2; + if (s1.end() == s1_iter) + return (s2.end() == s2_iter) ? 0 : -1; + else if (s2.end() == s2_iter) + return 1; + + const int c1 = tolower(uint8_t(*s1_iter++)); + const int c2 = tolower(uint8_t(*s2_iter++)); + const int diff = c1 - c2; + if (diff) + return diff; } } diff --git a/src/lib/util/corestr.h b/src/lib/util/corestr.h index b000d76731c..d23609a20c1 100644 --- a/src/lib/util/corestr.h +++ b/src/lib/util/corestr.h @@ -25,7 +25,7 @@ ***************************************************************************/ /* since stricmp is not part of the standard, we use this instead */ -int core_stricmp(const char *s1, const char *s2); +int core_stricmp(std::string_view s1, std::string_view s2); /* this macro prevents people from using stricmp directly */ #undef stricmp diff --git a/src/osd/modules/font/font_sdl.cpp b/src/osd/modules/font/font_sdl.cpp index 6c97f0a71f0..ee3024177d4 100644 --- a/src/osd/modules/font/font_sdl.cpp +++ b/src/osd/modules/font/font_sdl.cpp @@ -351,10 +351,10 @@ bool font_sdl::get_font_families(std::string const &font_path, std::vector const &a, std::pair const &b) -> bool { - int const second = core_stricmp(a.second.c_str(), b.second.c_str()); + int const second = core_stricmp(a.second, b.second); if (second < 0) return true; else if (second > 0) return false; - else return core_stricmp(b.first.c_str(), b.first.c_str()) < 0; + else return core_stricmp(b.first, b.first) < 0; }; std::string config((const char *)val.u.s); std::string display(config); diff --git a/src/osd/modules/font/font_windows.cpp b/src/osd/modules/font/font_windows.cpp index 8e0bd568349..b11ffe75111 100644 --- a/src/osd/modules/font/font_windows.cpp +++ b/src/osd/modules/font/font_windows.cpp @@ -109,7 +109,7 @@ bool osd_font_windows::open(std::string const &font_path, std::string const &_na // if it doesn't match our request, fail std::string utf = osd::text::from_tstring(&realname[0]); - int result = core_stricmp(utf.c_str(), name.c_str()); + int result = core_stricmp(utf, name); // if we didn't match, nuke our font and fall back if (result != 0) diff --git a/src/tools/imgtool/library.cpp b/src/tools/imgtool/library.cpp index f53db6fd4a7..3b05d947a49 100644 --- a/src/tools/imgtool/library.cpp +++ b/src/tools/imgtool/library.cpp @@ -264,7 +264,7 @@ int library::module_compare(const imgtool_module *m1, const imgtool_module *m2, rc = strcmp(m1->name.c_str(), m2->name.c_str()); break; case sort_type::DESCRIPTION: - rc = core_stricmp(m1->description.c_str(), m2->description.c_str()); + rc = core_stricmp(m1->description, m2->description); break; } return rc; diff --git a/src/tools/imgtool/modules/amiga.cpp b/src/tools/imgtool/modules/amiga.cpp index ec7d57ff450..08018a88f82 100644 --- a/src/tools/imgtool/modules/amiga.cpp +++ b/src/tools/imgtool/modules/amiga.cpp @@ -255,16 +255,22 @@ static int intl_toupper(int c) /* Amiga filename case insensitive string compare */ -static int intl_stricmp(const char *s1, const char *s2) +static int intl_stricmp(std::string_view s1, std::string_view s2) { - for (;;) + auto s1_iter = s1.begin(); + auto s2_iter = s2.begin(); + while (true) { - int c1 = intl_toupper(*s1++); - int c2 = intl_toupper(*s2++); + if (s1.end() == s1_iter) + return (s2.end() == s2_iter) ? 0 : -1; + else if (s2.end() == s2_iter) + return 1; - if (c1 == 0 || c1 != c2) - return c1 - c2; - } + const int c1 = intl_toupper(uint8_t(*s1_iter++)); + const int c2 = intl_toupper(uint8_t(*s2_iter++)); + const int diff = c1 - c2; + if (diff) + return diff; } @@ -971,7 +977,7 @@ static imgtoolerr_t walk_hash_chain(imgtool::image &img, const char *path, int s char name[31]; /* choose compare function depending on intl mode */ - int (*cmp)(const char *, const char *) = is_intl(img) ? &intl_stricmp : &core_stricmp; + int (*cmp)(std::string_view, std::string_view) = is_intl(img) ? &intl_stricmp : &core_stricmp; /* initialize filenames */ memset(name, 0, sizeof(name)); diff --git a/src/tools/imgtool/modules/dgndos.cpp b/src/tools/imgtool/modules/dgndos.cpp index 13fbbfc55ef..147e8e8a6c7 100644 --- a/src/tools/imgtool/modules/dgndos.cpp +++ b/src/tools/imgtool/modules/dgndos.cpp @@ -137,7 +137,7 @@ static imgtoolerr_t lookup_dgndos_file(uint8_t *entire_track, const char *fname, fnamebuf = get_dirent_fname(ent); } - while(core_stricmp(fnamebuf.c_str(), fname)); + while(core_stricmp(fnamebuf, fname)); if (position) *position = i - 1; diff --git a/src/tools/imgtool/modules/rsdos.cpp b/src/tools/imgtool/modules/rsdos.cpp index 09b8131fa30..c07e215f2da 100644 --- a/src/tools/imgtool/modules/rsdos.cpp +++ b/src/tools/imgtool/modules/rsdos.cpp @@ -108,7 +108,7 @@ static imgtoolerr_t lookup_rsdos_file(imgtool::image &f, const char *fname, rsdo if (ent.filename[0] != -1) fnamebuf = get_dirent_fname(ent); } - while((ent.filename[0] != -1) && core_stricmp(fnamebuf.c_str(), fname)); + while((ent.filename[0] != -1) && core_stricmp(fnamebuf, fname)); if (ent.filename[0] == -1) return IMGTOOLERR_FILENOTFOUND; -- cgit v1.2.3