ImStrv: using length(), fix ambiguous empty() function, fix altered behaviors, removed unused operators.

This commit is contained in:
ocornut 2020-11-26 22:37:38 +01:00
parent 7abe463801
commit 4e894ae1d9
6 changed files with 79 additions and 87 deletions

View File

@ -1510,6 +1510,15 @@ ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c,
// [SECTION] MISC HELPERS/UTILITIES (String, Format, Hash functions)
//-----------------------------------------------------------------------------
int ImStrcmp(ImStrv str1, ImStrv str2)
{
size_t str1_len = str1.length();
size_t str2_len = str2.length();
if (str1_len != str2_len)
return (int)str1_len - (int)str2_len;
return memcmp(str1.Begin, str2.Begin, str1_len);
}
// Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more.
int ImStricmp(const char* str1, const char* str2)
{
@ -1538,7 +1547,7 @@ void ImStrncpy(char* dst, ImStrv src, size_t count)
{
// Even though src does not necessarily include \0 terminator it is ok to include it. ImStrncpy above does not
// actually include that in a copy operation and inserts zero terminator manually.
ImStrncpy(dst, src.Begin, ImMin(count, IM_IMSTR_LENGTH(src) + 1));
ImStrncpy(dst, src.Begin, ImMin(count, src.length() + 1));
}
char* ImStrdup(const char* str)
@ -1550,7 +1559,7 @@ char* ImStrdup(const char* str)
char* ImStrdup(ImStrv str)
{
size_t len = IM_IMSTR_LENGTH(str);
size_t len = str.length();
void* buf = IM_ALLOC(len + 1);
*((char*)buf + len) = 0; // str may not contain \0, it must be inserted manually.
if (len > 0)
@ -1561,7 +1570,7 @@ char* ImStrdup(ImStrv str)
char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStrv src)
{
size_t dst_buf_size = p_dst_size ? *p_dst_size : strlen(dst) + 1;
size_t src_size = IM_IMSTR_LENGTH(src) + 1;
size_t src_size = src.length() + 1;
if (dst_buf_size < src_size)
{
IM_FREE(dst);
@ -1630,6 +1639,7 @@ const char* ImStristr(const char* haystack, const char* haystack_end, const char
return NULL;
}
// FIXME-IMSTR: probably unneeded.
const char* ImStrstr(ImStrv haystack, ImStrv needle)
{
const char un0 = (char)*needle.Begin;
@ -1774,7 +1784,7 @@ ImGuiID ImHashStr(ImStrv str, ImU32 seed)
const ImU32* crc32_lut = GCrc32LookupTable;
if (str.End != NULL)
{
size_t data_size = IM_IMSTR_LENGTH(str);
size_t data_size = str.length();
while (data_size-- != 0)
{
unsigned char c = *data++;
@ -1807,20 +1817,21 @@ ImFileHandle ImFileOpen(ImStrv filename, ImStrv mode)
#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(__CYGWIN__) && !defined(__GNUC__)
// We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames.
// Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32!
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)IM_IMSTR_LENGTH(filename) + 1, NULL, 0);
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)IM_IMSTR_LENGTH(mode) + 1, NULL, 0);
const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length(), NULL, 0);
const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length(), NULL, 0);
ImVector<ImWchar> buf;
buf.resize(filename_wsize + mode_wsize);
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)IM_IMSTR_LENGTH(filename) + 1, (wchar_t*)&buf[0], filename_wsize);
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)IM_IMSTR_LENGTH(mode) + 1, (wchar_t*)&buf[filename_wsize], mode_wsize);
return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]);
buf.resize(filename_wsize + 1 + mode_wsize + 1);
::MultiByteToWideChar(CP_UTF8, 0, filename.Begin, (int)filename.length(), (wchar_t*)&buf[0], filename_wsize);
::MultiByteToWideChar(CP_UTF8, 0, mode.Begin, (int)mode.length(), (wchar_t*)&buf[filename_wsize + 1], mode_wsize);
buf[filename_wsize] = buf[filename_wsize + 1 + mode_wsize] = 0;
return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize + 1]);
#else
// ImStrv is not guaranteed to be zero-terminated.
ImStrv filename_0 = ImStrdup(filename);
ImStrv mode_0 = ImStrdup(mode);
ImFileHandle handle = fopen(filename_0.Begin, mode_0.Begin);
IM_FREE(const_cast<char*>(filename_0.Begin));
IM_FREE(const_cast<char*>(mode_0.Begin));
IM_FREE((char*)(void*)filename_0.Begin);
IM_FREE((char*)(void*)mode_0.Begin);
return handle;
#endif
}
@ -2360,7 +2371,7 @@ bool ImGuiTextFilter::PassFilter(ImStrv text) const
if (Filters.empty())
return true;
if (text.Empty()) // FIXME-IMSTR
if (!text)
text.Begin = text.End = "";
for (int i = 0; i != Filters.Size; i++)
@ -2407,7 +2418,7 @@ char ImGuiTextBuffer::EmptyString[1] = { 0 };
void ImGuiTextBuffer::append(ImStrv str)
{
int len = (int)IM_IMSTR_LENGTH(str);
int len = (int)str.length();
// Add zero-terminator the first time
const int write_off = (Buf.Size != 0) ? Buf.Size : 1;
@ -3273,7 +3284,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, ImStrv name) : DrawListInst(NULL
{
memset(this, 0, sizeof(*this));
Name = ImStrdup(name);
NameBufLen = (int)IM_IMSTR_LENGTH(name) + 1;
NameBufLen = (int)name.length() + 1;
ID = ImHashStr(name);
IDStack.push_back(ID);
MoveId = GetID("#MOVE");
@ -3695,7 +3706,7 @@ void ImGui::SetClipboardText(ImStrv text)
ImGuiContext& g = *GImGui;
if (g.IO.SetClipboardTextFn)
{
int len = (int)IM_IMSTR_LENGTH(text);
int len = (int)text.length();
char* text_p = (char*)IM_ALLOC(len + 1);
if (len > 0)
memcpy(text_p, text.Begin, len);
@ -5233,7 +5244,7 @@ bool ImGui::BeginChildEx(ImStrv name, ImGuiID id, const ImVec2& size_arg, bool b
// Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value.
if (name)
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%.*s_%08X", parent_window->Name, (int)IM_IMSTR_LENGTH(name), name.Begin, id);
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%.*s_%08X", parent_window->Name, (int)name.length(), name.Begin, id);
else
ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%08X", parent_window->Name, id);
@ -6028,7 +6039,7 @@ bool ImGui::Begin(ImStrv name, bool* p_open, ImGuiWindowFlags flags)
{
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
IM_ASSERT(!name.Empty()); // Window name required
IM_ASSERT(name.Begin != NULL); // Window name required
IM_ASSERT(g.WithinFrameScope); // Forgot to call ImGui::NewFrame()
IM_ASSERT(g.FrameCountEnded != g.FrameCount); // Called ImGui::Render() or ImGui::EndFrame() and haven't called ImGui::NewFrame() again yet
@ -6185,7 +6196,7 @@ bool ImGui::Begin(ImStrv name, bool* p_open, ImGuiWindowFlags flags)
bool window_title_visible_elsewhere = false;
if (g.NavWindowingListWindow != NULL && (window->Flags & ImGuiWindowFlags_NoNavFocus) == 0) // Window titles visible when using CTRL+TAB
window_title_visible_elsewhere = true;
if (window_title_visible_elsewhere && !window_just_created && name != ImStrv(window->Name))
if (window_title_visible_elsewhere && !window_just_created && ImStrcmp(name, window->Name) != 0)
{
size_t buf_len = (size_t)window->NameBufLen;
window->Name = ImStrdupcpy(window->Name, &buf_len, name);
@ -8010,7 +8021,7 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs)
bool ImGui::DebugCheckVersionAndDataLayout(ImStrv version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert, size_t sz_idx)
{
bool error = false;
if (version != ImStrv(IMGUI_VERSION)) { error = true; IM_ASSERT(version == ImStrv(IMGUI_VERSION) && "Mismatched version string!"); }
if (ImStrcmp(version, IMGUI_VERSION) != 0) { error = true; IM_ASSERT(0 && "Mismatched version string!"); }
if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); }
if (sz_style != sizeof(ImGuiStyle)) { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle) && "Mismatched struct layout!"); }
if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); }
@ -11168,8 +11179,8 @@ bool ImGui::SetDragDropPayload(ImStrv type, const void* data, size_t data_size,
if (cond == 0)
cond = ImGuiCond_Always;
IM_ASSERT(!type.Empty() && "Payload type can not be empty");
IM_ASSERT(IM_IMSTR_LENGTH(type) < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long");
IM_ASSERT(type.Begin != NULL && "Payload type can not be empty");
IM_ASSERT(type.End - type.Begin < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long");
IM_ASSERT((data != NULL && data_size > 0) || (data == NULL && data_size == 0));
IM_ASSERT(cond == ImGuiCond_Always || cond == ImGuiCond_Once);
IM_ASSERT(payload.SourceId != 0); // Not called between BeginDragDropSource() and EndDragDropSource()
@ -11468,9 +11479,9 @@ void ImGui::LogToFile(int auto_open_depth, ImStrv filename)
// FIXME: We could probably open the file in text mode "at", however note that clipboard/buffer logging will still
// be subject to outputting OS-incompatible carriage return if within strings the user doesn't use IM_NEWLINE.
// By opening the file in binary mode "ab" we have consistent output everywhere.
if (filename.Empty())
if (!filename)
filename = g.IO.LogFilename;
if (filename.Empty())
if (filename.empty())
return;
ImFileHandle f = ImFileOpen(filename, "ab");
if (!f)
@ -11627,7 +11638,7 @@ void ImGui::MarkIniSettingsDirty(ImGuiWindow* window)
ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStrv name)
{
ImGuiContext& g = *GImGui;
const size_t name_len = IM_IMSTR_LENGTH(name);
const size_t name_len = name.length();
if (!name_len)
{
IM_ASSERT(false && "Name must not be empty.");
@ -11708,7 +11719,7 @@ void ImGui::LoadIniSettingsFromMemory(ImStrv ini_data, size_t ini_size)
// For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter).
// For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy..
if (ini_size == 0)
ini_size = IM_IMSTR_LENGTH(ini_data);
ini_size = ini_data.length();
g.SettingsIniData.Buf.resize((int)ini_size + 1);
char* const buf = g.SettingsIniData.Buf.Data;
char* const buf_end = buf + ini_size;

28
imgui.h
View File

@ -269,8 +269,6 @@ struct ImVec4
#endif
};
#define IM_IMSTR_LENGTH(s) (size_t)(s.End - s.Begin)
// String view class.
#define IMGUI_HAS_IMSTR
struct ImStrv
@ -281,27 +279,9 @@ struct ImStrv
ImStrv(const char* b) { Begin = b; End = b ? b + strlen(b) : NULL; }
ImStrv(const char* b, const char* e){ Begin = b; End = e ? e : b + strlen(b); }
ImStrv(const char* b, size_t size) { Begin = b; End = b + size; }
bool Empty() const { return Begin == End || Begin[0] == 0; } // FIXME: Ambiguous
// void EnsureHasEnd() { if (End == NULL) End = Begin + Length(); }
// size_t Length() const
// {
// if (Begin == NULL)
// return 0;
// if (End == NULL)
// return strlen(Begin);
// return (size_t)(End - Begin);
// }
bool operator==(ImStrv other) const
{
if (Begin == other.Begin && End == other.End)
return true;
size_t len = IM_IMSTR_LENGTH((*this));
if (len == IM_IMSTR_LENGTH(other))
return memcmp(Begin, other.Begin, len) == 0;
return false;
}
inline operator bool() const { return Begin != NULL; }
inline char operator[](int index) const { return Begin[index]; }
inline size_t length() const { return (size_t)(End - Begin); }
inline bool empty() const { return Begin == End; } // == "" or == NULL
inline operator bool() const { return Begin != NULL; } // != NULL
#ifdef IM_IMSTR_CLASS_EXTRA
IM_IMSTR_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your string types and ImStrv.
#endif
@ -2229,7 +2209,7 @@ struct ImGuiTextFilter
void Clear() { InputBuf[0] = 0; Build(); }
bool IsActive() const { return !Filters.empty(); }
// [Internal]
// [Internal] FIXME-IMSTR: replace this with ImStrv, remove split as an internal function
struct ImGuiTextRange
{
const char* b;

View File

@ -1577,8 +1577,7 @@ void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos,
{
if ((col & IM_COL32_A_MASK) == 0)
return;
if (text.Empty())
if (text.empty())
return;
// Pull default font/size from the shared ImDrawListSharedData instance
@ -2108,11 +2107,13 @@ static const char* GetDefaultCompressedFontDataTTFBase85();
static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; }
static void Decode85(ImStrv src, unsigned char* dst)
{
while (!src.Empty())
const char* p = src.Begin;
const char* p_end = src.End;
while (p < p_end)
{
unsigned int tmp = Decode85Byte(src[0]) + 85 * (Decode85Byte(src[1]) + 85 * (Decode85Byte(src[2]) + 85 * (Decode85Byte(src[3]) + 85 * Decode85Byte(src[4]))));
unsigned int tmp = Decode85Byte(p[0]) + 85 * (Decode85Byte(p[1]) + 85 * (Decode85Byte(p[2]) + 85 * (Decode85Byte(p[3]) + 85 * Decode85Byte(p[4]))));
dst[0] = ((tmp >> 0) & 0xFF); dst[1] = ((tmp >> 8) & 0xFF); dst[2] = ((tmp >> 16) & 0xFF); dst[3] = ((tmp >> 24) & 0xFF); // We can't assume little-endianness.
src.Begin += 5;
p += 5;
dst += 4;
}
}
@ -2154,9 +2155,9 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(ImStrv filename, float size_pixels, cons
{
// Store a short copy of filename into into the font name for convenience
const char* p;
for (p = filename.Begin + IM_IMSTR_LENGTH(filename); p > filename.Begin && p[-1] != '/' && p[-1] != '\\'; p--) {}
for (p = filename.End; p > filename.Begin && p[-1] != '/' && p[-1] != '\\'; p--) {}
filename.Begin = p;
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s, %.0fpx", (int)IM_IMSTR_LENGTH(filename), filename.Begin, size_pixels);
ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%.*s, %.0fpx", (int)filename.length(), filename.Begin, size_pixels);
}
return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges);
}
@ -2189,7 +2190,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_d
ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(ImStrv compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges)
{
int compressed_ttf_size = (((int)IM_IMSTR_LENGTH(compressed_ttf_data_base85) + 4) / 5) * 4;
int compressed_ttf_size = (((int)compressed_ttf_data_base85.length() + 4) / 5) * 4;
void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size);
Decode85(compressed_ttf_data_base85, (unsigned char*)compressed_ttf);
ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges);
@ -3065,7 +3066,7 @@ const ImWchar* ImFontAtlas::GetGlyphRangesVietnamese()
void ImFontGlyphRangesBuilder::AddText(ImStrv text)
{
while (!text.Empty())
while (!text.empty())
{
unsigned int c = 0;
int c_len = ImTextCharFromUtf8(&c, text.Begin, text.End);

View File

@ -315,6 +315,8 @@ static inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v &
static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
// Helpers: String, Formatting
static inline int ImStrcmp(const char* str1, const char* str2) { return strcmp(str1, str2); }
IMGUI_API int ImStrcmp(ImStrv str1, ImStrv str2);
IMGUI_API int ImStricmp(const char* str1, const char* str2);
IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count);
IMGUI_API void ImStrncpy(char* dst, ImStrv src, size_t count);
@ -339,8 +341,6 @@ IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* b
IMGUI_API int ImParseFormatPrecision(const char* format, int default_value);
static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; }
static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; }
static inline size_t ImStrlen(const char* str) { return strlen(str); }
static inline size_t ImStrlen(const ImWchar* str) { const ImWchar* e = str; while (*e) ++e; return (size_t)(e - str); }
// Helpers: UTF-8 <> wchar conversions
IMGUI_API const char* ImTextCharToUtf8(char out_buf[5], unsigned int c); // return out_buf

View File

@ -1454,11 +1454,11 @@ void ImGui::TableSetupColumn(ImStrv label, ImGuiTableColumnFlags flags, float in
// Store name (append with zero-terminator in contiguous buffer)
column->NameOffset = -1;
if (label != NULL && label[0] != 0)
if (!label.empty())
{
char zero_terminator = 0;
column->NameOffset = (ImS16)table->ColumnsNames.size();
table->ColumnsNames.append(label.Begin, label.Begin + IM_IMSTR_LENGTH(label));
table->ColumnsNames.append(label.Begin, label.End);
table->ColumnsNames.append(&zero_terminator, &zero_terminator + 1);
}
}

View File

@ -151,14 +151,14 @@ static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const
void ImGui::TextEx(ImStrv text, ImGuiTextFlags flags)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems || text.Empty())
if (window->SkipItems)
return;
ImGuiContext& g = *GImGui;
const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
const float wrap_pos_x = window->DC.TextWrapPos;
const bool wrap_enabled = (wrap_pos_x >= 0.0f);
if (IM_IMSTR_LENGTH(text) > 2000 && !wrap_enabled)
if (text.End - text.Begin > 2000 && !wrap_enabled)
{
// Long text!
// Perform manual coarse clipping to optimize for long multi-line text
@ -1623,7 +1623,7 @@ bool ImGui::BeginCombo(ImStrv label, ImStrv preview_value, ImGuiComboFlags flags
}
// Render preview and label
if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview))
if (!preview_value.empty() && !(flags & ImGuiComboFlags_NoPreview))
{
if (g.LogEnabled)
LogSetNextTextDecoration("{", "}");
@ -2348,13 +2348,13 @@ bool ImGui::DragScalar(ImStrv label, ImGuiDataType data_type, void* p_data, floa
char format_0[64]; // format may not end with \0
const char* format = format_0;
IM_ASSERT(IM_IMSTR_LENGTH(format_p) < IM_ARRAYSIZE(format_0));
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0));
// Default format string when passing NULL
if (format_p.Empty())
if (!format_p)
format = DataTypeGetInfo(data_type)->PrintFmt;
else if (data_type == ImGuiDataType_S32 && format_p != ImStrv("%d")) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
else if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
format = PatchFormatStringFloatToInt(format);
// Tabbing or CTRL-clicking on Drag turns it into an InputText
@ -2971,15 +2971,15 @@ bool ImGui::SliderScalar(ImStrv label, ImGuiDataType data_type, void* p_data, co
char format_0[64]; // format may not end with \0
const char* format = format_0;
IM_ASSERT(IM_IMSTR_LENGTH(format_p) < IM_ARRAYSIZE(format_0));
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
// Default format string when passing NULL
if (format_p.Empty())
if (!format_p)
format = DataTypeGetInfo(data_type)->PrintFmt;
else
{
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0));
if (data_type == ImGuiDataType_S32 && format_p != ImStrv("%d")) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
format = PatchFormatStringFloatToInt(format);
}
@ -3095,7 +3095,7 @@ bool ImGui::SliderFloat4(ImStrv label, float v[4], float v_min, float v_max, ImS
bool ImGui::SliderAngle(ImStrv label, float* v_rad, float v_degrees_min, float v_degrees_max, ImStrv format, ImGuiSliderFlags flags)
{
if (format.Empty())
if (!format)
format = "%.0f deg";
float v_deg = (*v_rad) * 360.0f / (2 * IM_PI);
bool value_changed = SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, format, flags);
@ -3143,15 +3143,15 @@ bool ImGui::VSliderScalar(ImStrv label, const ImVec2& size, ImGuiDataType data_t
char format_0[64]; // format may not end with \0
const char* format = format_0;
IM_ASSERT(IM_IMSTR_LENGTH(format_p) < IM_ARRAYSIZE(format_0));
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
// Default format string when passing NULL
if (format_p.Empty())
if (!format_p)
format = DataTypeGetInfo(data_type)->PrintFmt;
else
{
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0));
if (data_type == ImGuiDataType_S32 && format_p != ImStrv("%d")) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
if (data_type == ImGuiDataType_S32 && ImStrcmp(format_p, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
format = PatchFormatStringFloatToInt(format);
}
@ -3393,10 +3393,10 @@ bool ImGui::InputScalar(ImStrv label, ImGuiDataType data_type, void* p_data, con
char format_0[64]; // format may not end with \0
const char* format = format_0;
IM_ASSERT(IM_IMSTR_LENGTH(format_p) < IM_ARRAYSIZE(format_0));
IM_ASSERT(format_p.End - format_p.Begin < IM_ARRAYSIZE(format_0));
ImStrncpy(format_0, format_p, IM_ARRAYSIZE(format_0));
if (format_p.Empty())
if (!format_p)
format = DataTypeGetInfo(data_type)->PrintFmt;
char buf[64];
@ -3791,7 +3791,7 @@ void ImGuiInputTextCallbackData::DeleteChars(int pos, int bytes_count)
void ImGuiInputTextCallbackData::InsertChars(int pos, ImStrv new_text)
{
const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0;
const int new_text_len = (int)IM_IMSTR_LENGTH(new_text);
const int new_text_len = (int)new_text.length();
if (new_text_len + BufTextLen >= BufSize)
{
if (!is_resizable)
@ -4122,7 +4122,7 @@ bool ImGui::InputTextEx(ImStrv label, ImStrv hint, char* buf, int buf_size, cons
// Select the buffer to render.
const bool buf_display_from_state = (render_cursor || render_selection || g.ActiveId == id) && !is_readonly && state && state->TextAIsValid;
const bool is_displaying_hint = (!hint.Empty() && (buf_display_from_state ? state->TextA.Data : buf)[0] == 0);
const bool is_displaying_hint = (!hint.empty() && (buf_display_from_state ? state->TextA.Data : buf)[0] == 0);
// Password pushes a temporary font with only a fallback glyph
if (is_password && !is_displaying_hint)
@ -4358,7 +4358,7 @@ bool ImGui::InputTextEx(ImStrv label, ImStrv hint, char* buf, int buf_size, cons
if (ImStrv clipboard = GetClipboardText())
{
// Filter pasted buffer
const int clipboard_len = (int)IM_IMSTR_LENGTH(clipboard);
const int clipboard_len = (int)clipboard.length();
ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len + 1) * sizeof(ImWchar));
int clipboard_filtered_len = 0;
for (const char* s = clipboard.Begin; *s; )
@ -6612,17 +6612,17 @@ void ImGui::PlotHistogram(ImStrv label, float (*values_getter)(void* data, int i
void ImGui::Value(ImStrv prefix, bool b)
{
Text("%.*s: %s", (int)IM_IMSTR_LENGTH(prefix), prefix.Begin, (b ? "true" : "false"));
Text("%.*s: %s", (int)prefix.length(), prefix.Begin, (b ? "true" : "false"));
}
void ImGui::Value(ImStrv prefix, int v)
{
Text("%.*s: %d", (int)IM_IMSTR_LENGTH(prefix), prefix.Begin, v);
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
}
void ImGui::Value(ImStrv prefix, unsigned int v)
{
Text("%.*s: %d", (int)IM_IMSTR_LENGTH(prefix), prefix.Begin, v);
Text("%.*s: %d", (int)prefix.length(), prefix.Begin, v);
}
void ImGui::Value(ImStrv prefix, float v, ImStrv float_format)
@ -6630,12 +6630,12 @@ void ImGui::Value(ImStrv prefix, float v, ImStrv float_format)
if (float_format)
{
char fmt[64];
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", (int)IM_IMSTR_LENGTH(float_format), float_format.Begin);
Text(fmt, (int)IM_IMSTR_LENGTH(prefix), prefix.Begin, v);
ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%.*s: %.*s", (int)float_format.length(), float_format.Begin);
Text(fmt, (int)prefix.length(), prefix.Begin, v);
}
else
{
Text("%.*s: %.3f", (int)IM_IMSTR_LENGTH(prefix), prefix.Begin, v);
Text("%.*s: %.3f", (int)prefix.length(), prefix.Begin, v);
}
}