ImStrv: rework toward ensuring End is always set to constant can be compile time calculated

This commit is contained in:
ocornut
2020-11-26 22:17:55 +01:00
parent 35c0f9a29b
commit 7abe463801
4 changed files with 11 additions and 33 deletions

19
imgui.h
View File

@ -269,8 +269,7 @@ struct ImVec4
#endif
};
#define IM_IMSTR_LENGTH(s) (s.Begin ? (s.End ? (size_t)(s.End - s.Begin) : strlen(s.Begin)) : 0)
#define IM_IMSTR_ENSURE_HAS_END(s) if (s.End == NULL) s.End = s.Begin + strlen(s.Begin)
#define IM_IMSTR_LENGTH(s) (size_t)(s.End - s.Begin)
// String view class.
#define IMGUI_HAS_IMSTR
@ -278,11 +277,11 @@ struct ImStrv
{
const char* Begin;
const char* End;
ImStrv() { Begin = End = NULL; }
ImStrv(const char* b) { Begin = b; End = NULL; }
ImStrv(const char* b, const char* e) { Begin = b; End = e; }
ImStrv(const char* b, size_t size) { Begin = b; End = b + size; }
bool Empty() const { return Begin == NULL || Begin == End || Begin[0] == 0; }
ImStrv() { Begin = End = NULL; }
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
// {
@ -301,10 +300,10 @@ struct ImStrv
return memcmp(Begin, other.Begin, len) == 0;
return false;
}
operator bool() const { return Begin != NULL; }
char operator[](int index) const { return Begin[index]; }
inline operator bool() const { return Begin != NULL; }
inline char operator[](int index) const { return Begin[index]; }
#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 math types and ImStrv.
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
};