Remove extraneous instances of .c_str().

Most of these were just converting char* into std::string back and
forth; some more used ReadUTF8, which was converted to use nicer
STL-style iterators over UTF-8 text.

The remaining ones are:
  * arguments to Expr::From, which we'll change when refactoring
    the expression lexer;
  * arguments to varargs functions, which we'll change when adding
    localization (that requires custom printf-style functions to
    allow for changing argument order);
  * arguments where only string literals are ever passed, which
    are OK;
  * in platform-specific code, which is OK.
pull/4/head
whitequark 2016-02-14 20:13:40 +00:00
parent a1a624da12
commit 5c15cbf5f6
12 changed files with 52 additions and 68 deletions

View File

@ -14,7 +14,7 @@ void TextWindow::ScreenEditTtfText(int link, uint32_t v) {
hRequest hr = { v }; hRequest hr = { v };
Request *r = SK.GetRequest(hr); Request *r = SK.GetRequest(hr);
SS.TW.ShowEditControl(10, r->str.c_str()); SS.TW.ShowEditControl(10, r->str);
SS.TW.edit.meaning = EDIT_TTF_TEXT; SS.TW.edit.meaning = EDIT_TTF_TEXT;
SS.TW.edit.request = hr; SS.TW.edit.request = hr;
} }

View File

@ -124,7 +124,7 @@ void Constraint::DoLabel(Vector ref, Vector *labelPos, Vector gr, Vector gu) {
if(dogd.drawing) { if(dogd.drawing) {
ssglWriteTextRefCenter(s.c_str(), th, ref, gr, gu, LineCallback, this); ssglWriteTextRefCenter(s, th, ref, gr, gu, LineCallback, this);
} else { } else {
double l = swidth/2 - sheight/2; double l = swidth/2 - sheight/2;
l = max(l, 5/SS.GW.scale); l = max(l, 5/SS.GW.scale);

View File

@ -447,7 +447,7 @@ void Entity::GenerateBezierCurves(SBezierList *sbl) {
Vector v = topLeft.Minus(botLeft); Vector v = topLeft.Minus(botLeft);
Vector u = (v.Cross(n)).WithMagnitude(v.Magnitude()); Vector u = (v.Cross(n)).WithMagnitude(v.Magnitude());
SS.fonts.PlotString(font.c_str(), str.c_str(), sbl, botLeft, u, v); SS.fonts.PlotString(font, str, sbl, botLeft, u, v);
break; break;
} }
@ -610,7 +610,7 @@ void Entity::DrawOrGetDistance(void) {
std::string str = DescriptionString().substr(5); std::string str = DescriptionString().substr(5);
double th = DEFAULT_TEXT_HEIGHT; double th = DEFAULT_TEXT_HEIGHT;
if(dogd.drawing) { if(dogd.drawing) {
ssglWriteText(str.c_str(), th, mm2, u, v, NULL, NULL); ssglWriteText(str, th, mm2, u, v, NULL, NULL);
} else { } else {
Vector pos = mm2.Plus(u.ScaledBy(ssglStrWidth(str, th)/2)).Plus( Vector pos = mm2.Plus(u.ScaledBy(ssglStrWidth(str, th)/2)).Plus(
v.ScaledBy(ssglStrHeight(th)/2)); v.ScaledBy(ssglStrHeight(th)/2));

View File

@ -242,7 +242,7 @@ void SolveSpaceUI::GenerateAll(int first, int last, bool andFindFree, bool genFo
glPushMatrix(); glPushMatrix();
glTranslated(left+8, top-20, 0); glTranslated(left+8, top-20, 0);
glScaled(1, -1, 1); glScaled(1, -1, 1);
ssglBitmapText(msg.c_str(), Vector::From(0, 0, 0)); ssglBitmapText(msg, Vector::From(0, 0, 0));
glPopMatrix(); glPopMatrix();
glFlush(); glFlush();
glDrawBuffer(GL_BACK); glDrawBuffer(GL_BACK);

View File

@ -40,11 +40,7 @@ static const VectorGlyph &GetVectorGlyph(char32_t chr) {
double ssglStrWidth(const std::string &str, double h) double ssglStrWidth(const std::string &str, double h)
{ {
int width = 0; int width = 0;
const char *iter = str.c_str(); for(char32_t chr : ReadUTF8(str)) {
while(*iter) {
char32_t chr;
iter = ReadUTF8(iter, &chr);
const VectorGlyph &glyph = GetVectorGlyph(chr); const VectorGlyph &glyph = GetVectorGlyph(chr);
if(glyph.baseCharacter != 0) { if(glyph.baseCharacter != 0) {
const VectorGlyph &baseGlyph = GetVectorGlyph(glyph.baseCharacter); const VectorGlyph &baseGlyph = GetVectorGlyph(glyph.baseCharacter);
@ -149,11 +145,7 @@ void ssglWriteText(const std::string &str, double h, Vector t, Vector u, Vector
double scale = FONT_SCALE(h) / SS.GW.scale; double scale = FONT_SCALE(h) / SS.GW.scale;
Vector o = { 5.0, 5.0 }; Vector o = { 5.0, 5.0 };
const char *iter = str.c_str(); for(char32_t chr : ReadUTF8(str)) {
while(*iter) {
char32_t chr;
iter = ReadUTF8(iter, &chr);
const VectorGlyph &glyph = GetVectorGlyph(chr); const VectorGlyph &glyph = GetVectorGlyph(chr);
o.x += ssglDrawCharacter(glyph, t, o, u, v, scale, fn, fndata); o.x += ssglDrawCharacter(glyph, t, o, u, v, scale, fn, fndata);
} }
@ -678,14 +670,11 @@ void ssglBitmapCharQuad(char32_t chr, double x, double y)
} }
} }
void ssglBitmapText(const char *str, Vector p) void ssglBitmapText(const std::string &str, Vector p)
{ {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); glBegin(GL_QUADS);
while(*str) { for(char32_t chr : ReadUTF8(str)) {
char32_t chr;
str = ReadUTF8(str, &chr);
ssglBitmapCharQuad(chr, p.x, p.y); ssglBitmapCharQuad(chr, p.x, p.y);
p.x += 8 * ssglBitmapCharWidth(chr); p.x += 8 * ssglBitmapCharWidth(chr);
} }

View File

@ -399,7 +399,7 @@ bool SolveSpaceUI::Autosave()
void SolveSpaceUI::RemoveAutosave() void SolveSpaceUI::RemoveAutosave()
{ {
std::string autosaveFile = saveFile + AUTOSAVE_SUFFIX; std::string autosaveFile = saveFile + AUTOSAVE_SUFFIX;
ssremove(autosaveFile.c_str()); ssremove(autosaveFile);
} }
bool SolveSpaceUI::OkayToStartNewFile(void) { bool SolveSpaceUI::OkayToStartNewFile(void) {

View File

@ -310,6 +310,25 @@ typedef IdList<Param,hParam> ParamList;
// Utility functions that are provided in the platform-independent code. // Utility functions that are provided in the platform-independent code.
class utf8_iterator : std::iterator<std::forward_iterator_tag, char32_t> {
const char *p, *n;
public:
utf8_iterator(const char *p) : p(p), n(NULL) {}
bool operator==(const utf8_iterator &i) const { return p==i.p; }
bool operator!=(const utf8_iterator &i) const { return p!=i.p; }
ptrdiff_t operator- (const utf8_iterator &i) const { return p -i.p; }
utf8_iterator& operator++() { **this; p=n; n=NULL; return *this; }
utf8_iterator operator++(int) { utf8_iterator t(*this); operator++(); return t; }
char32_t operator*();
};
class ReadUTF8 {
const std::string &str;
public:
ReadUTF8(const std::string &str) : str(str) {}
utf8_iterator begin() const { return utf8_iterator(&str[0]); }
utf8_iterator end() const { return utf8_iterator(&str[str.length()]); }
};
void ssglLineWidth(GLfloat width); void ssglLineWidth(GLfloat width);
void ssglVertex3v(Vector u); void ssglVertex3v(Vector u);
void ssglAxisAlignedQuad(double l, double r, double t, double b, bool lone = true); void ssglAxisAlignedQuad(double l, double r, double t, double b, bool lone = true);
@ -345,7 +364,7 @@ void ssglDepthRangeOffset(int units);
void ssglDepthRangeLockToFront(bool yes); void ssglDepthRangeLockToFront(bool yes);
void ssglDrawPixelsWithTexture(uint8_t *data, int w, int h); void ssglDrawPixelsWithTexture(uint8_t *data, int w, int h);
void ssglInitializeBitmapFont(); void ssglInitializeBitmapFont();
void ssglBitmapText(const char *str, Vector p); void ssglBitmapText(const std::string &str, Vector p);
void ssglBitmapCharQuad(char32_t chr, double x, double y); void ssglBitmapCharQuad(char32_t chr, double x, double y);
int ssglBitmapCharWidth(char32_t chr); int ssglBitmapCharWidth(char32_t chr);
#define TEXTURE_BACKGROUND_IMG 10 #define TEXTURE_BACKGROUND_IMG 10
@ -362,8 +381,6 @@ void MakeMatrix(double *mat, double a11, double a12, double a13, double a14,
double a31, double a32, double a33, double a34, double a31, double a32, double a33, double a34,
double a41, double a42, double a43, double a44); double a41, double a42, double a43, double a44);
bool MakeAcceleratorLabel(int accel, char *out); bool MakeAcceleratorLabel(int accel, char *out);
const char *ReadUTF8(const char *str, char32_t *chr);
bool StringAllPrintable(const char *str);
bool FilenameHasExtension(const std::string &str, const char *ext); bool FilenameHasExtension(const std::string &str, const char *ext);
void Message(const char *str, ...); void Message(const char *str, ...);
void Error(const char *str, ...); void Error(const char *str, ...);

View File

@ -476,7 +476,7 @@ void TextWindow::ShowListOfStyles(void) {
void TextWindow::ScreenChangeStyleName(int link, uint32_t v) { void TextWindow::ScreenChangeStyleName(int link, uint32_t v) {
hStyle hs = { v }; hStyle hs = { v };
Style *s = Style::Get(hs); Style *s = Style::Get(hs);
SS.TW.ShowEditControl(12, s->name.c_str()); SS.TW.ShowEditControl(12, s->name);
SS.TW.edit.style = hs; SS.TW.edit.style = hs;
SS.TW.edit.meaning = EDIT_STYLE_NAME; SS.TW.edit.meaning = EDIT_STYLE_NAME;
} }

View File

@ -248,20 +248,15 @@ void TextWindow::Printf(bool halfLine, const char *fmt, ...) {
break; break;
} }
} else { } else {
char32_t chr; utf8_iterator it2(fmt), it1 = it2++;
const char *fmtNext = ReadUTF8(fmt, &chr); strncpy(buf, fmt, it2 - it1);
strncpy(buf, fmt, fmtNext - fmt); buf[it2 - it1] = '\0';
buf[fmtNext - fmt] = '\0';
} }
const char *bufIter = buf; for(utf8_iterator it(buf); *it; ++it) {
while(*bufIter) { for(int i = 0; i < ssglBitmapCharWidth(*it); i++) {
char32_t chr;
bufIter = ReadUTF8(bufIter, &chr);
for(int i = 0; i < ssglBitmapCharWidth(chr); i++) {
if(c >= MAX_COLS) goto done; if(c >= MAX_COLS) goto done;
text[r][c] = (i == 0) ? chr : ' '; text[r][c] = (i == 0) ? *it : ' ';
meta[r][c].fg = fg; meta[r][c].fg = fg;
meta[r][c].bg = bg; meta[r][c].bg = bg;
meta[r][c].bgRgb = bgRgb; meta[r][c].bgRgb = bgRgb;
@ -463,7 +458,7 @@ void TextWindow::DrawOrHitTestIcons(int how, double mx, double my)
ssglAxisAlignedLineLoop(ox, ox+tw, oy, oy+LINE_HEIGHT); ssglAxisAlignedLineLoop(ox, ox+tw, oy, oy+LINE_HEIGHT);
glColor4d(0, 0, 0, 1); glColor4d(0, 0, 0, 1);
ssglBitmapText(str.c_str(), Vector::From(ox+5, oy-3+LINE_HEIGHT, 0)); ssglBitmapText(str, Vector::From(ox+5, oy-3+LINE_HEIGHT, 0));
} else { } else {
if(!hoveredIcon || if(!hoveredIcon ||
(hoveredIcon != tooltippedIcon)) (hoveredIcon != tooltippedIcon))

View File

@ -233,7 +233,7 @@ bool GraphicsWindow::ToolbarDrawOrHitTest(int mx, int my,
glPushMatrix(); glPushMatrix();
glTranslated(ox+5, oy+3, 0); glTranslated(ox+5, oy+3, 0);
glScaled(1, -1, 1); glScaled(1, -1, 1);
ssglBitmapText(str.c_str(), Vector::From(0, 0, 0)); ssglBitmapText(str, Vector::From(0, 0, 0));
glPopMatrix(); glPopMatrix();
} }
ssglDepthRangeLockToFront(false); ssglDepthRangeLockToFront(false);

View File

@ -213,12 +213,8 @@ static const FT_Outline_Funcs outline_funcs = {
void TtfFont::PlotString(const std::string &str, void TtfFont::PlotString(const std::string &str,
SBezierList *sbl, Vector origin, Vector u, Vector v) SBezierList *sbl, Vector origin, Vector u, Vector v)
{ {
const char *cstr = str.c_str();
FT_Pos dx = 0; FT_Pos dx = 0;
while(*cstr) { for(char32_t chr : ReadUTF8(str)) {
char32_t chr;
cstr = ReadUTF8(cstr, &chr);
uint32_t gid = FT_Get_Char_Index(fontFace, chr); uint32_t gid = FT_Get_Char_Index(fontFace, chr);
if (gid == 0) { if (gid == 0) {
dbp("freetype: CID-to-GID mapping for CID 0x%04x failed: %s; using CID as GID", dbp("freetype: CID-to-GID mapping for CID 0x%04x failed: %s; using CID as GID",

View File

@ -25,39 +25,26 @@ std::string SolveSpace::ssprintf(const char *fmt, ...)
return result; return result;
} }
// See https://github.com/GNOME/glibmm/blob/2fbd9f23/glib/glibmm/ustring.cc#L227 char32_t utf8_iterator::operator*()
const char *SolveSpace::ReadUTF8(const char *str, char32_t *result)
{ {
*result = (unsigned char) *str; const uint8_t *it = (const uint8_t*) this->p;
char32_t result = *it;
if((*result & 0x80) != 0) if((result & 0x80) != 0) {
{
unsigned int mask = 0x40; unsigned int mask = 0x40;
do do {
{ result <<= 6;
*result <<= 6; unsigned int c = (*++it);
const unsigned int c = (unsigned char) (*++str);
mask <<= 5; mask <<= 5;
*result += c - 0x80; result += c - 0x80;
} } while((result & mask) != 0);
while((*result & mask) != 0);
*result &= mask - 1; result &= mask - 1;
} }
return str + 1; this->n = (const char*) (it + 1);
} return result;
bool SolveSpace::StringAllPrintable(const char *str)
{
const char *t;
for(t = str; *t; t++) {
if(!(isalnum(*t) || *t == '-' || *t == '_')) {
return false;
}
}
return true;
} }
bool SolveSpace::FilenameHasExtension(const std::string &str, const char *ext) bool SolveSpace::FilenameHasExtension(const std::string &str, const char *ext)