[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] r3973 - trunk/pingus/src
From: |
grumbel at BerliOS |
Subject: |
[Pingus-CVS] r3973 - trunk/pingus/src |
Date: |
Thu, 26 Feb 2009 14:48:52 +0100 |
Author: grumbel
Date: 2009-02-26 14:48:51 +0100 (Thu, 26 Feb 2009)
New Revision: 3973
Modified:
trunk/pingus/src/font.cpp
trunk/pingus/src/string_format.cpp
trunk/pingus/src/string_format.hpp
trunk/pingus/src/utf8_iterator.cpp
trunk/pingus/src/utf8_iterator.hpp
Log:
Minor cleanup/restructor in UTF8::iterator
Modified: trunk/pingus/src/font.cpp
===================================================================
--- trunk/pingus/src/font.cpp 2009-02-26 12:33:31 UTC (rev 3972)
+++ trunk/pingus/src/font.cpp 2009-02-26 13:48:51 UTC (rev 3973)
@@ -109,7 +109,7 @@
float dstx = float(x - offset.x);
float dsty = float(y - offset.y);
- for(UTF8Iterator i(text); !i.done(); ++i)
+ for(UTF8::iterator i(text); !i.done(); ++i)
{
const uint32_t& unicode = *i;
@@ -144,7 +144,7 @@
{
float width = 0.0f;
float last_width = 0;
- for(UTF8Iterator i(text); !i.done(); ++i)
+ for(UTF8::iterator i(text); !i.done(); ++i)
{
const uint32_t& unicode = *i;
Modified: trunk/pingus/src/string_format.cpp
===================================================================
--- trunk/pingus/src/string_format.cpp 2009-02-26 12:33:31 UTC (rev 3972)
+++ trunk/pingus/src/string_format.cpp 2009-02-26 13:48:51 UTC (rev 3973)
@@ -20,7 +20,7 @@
#include "utf8_iterator.hpp"
std::string
-StringFormat::break_line (std::string text, int width, const Font& font)
+StringFormat::normalize(std::string text)
{
std::string::size_type pos = 0;
while ((pos = text.find('\t', pos)) != std::string::npos)
@@ -60,7 +60,14 @@
while ((pos = text.find(" ", pos)) != std::string::npos)
text.replace(pos, 2, 1, ' ');
- // Text is now normalized, time to start breaking the lines
+ return text;
+}
+
+std::string
+StringFormat::break_line (const std::string& text_, int width, const Font&
font)
+{
+ std::string text = StringFormat::normalize(text_);
+
std::string::const_iterator beg = text.begin();
int line_width = 0;
std::ostringstream out;
@@ -69,7 +76,7 @@
std::string word(beg, UTF8::advance(it));
int word_width = font.get_width(word);
- if
(UTF8::is_linebreak_character(UTF8Iterator::decode_utf8(std::string(it,
const_cast<const std::string&>(text).end()))))
+ if (UTF8::is_linebreak_character(UTF8::decode_utf8(std::string(it,
const_cast<const std::string&>(text).end()))))
{
if ((line_width + word_width) > width)
{
Modified: trunk/pingus/src/string_format.hpp
===================================================================
--- trunk/pingus/src/string_format.hpp 2009-02-26 12:33:31 UTC (rev 3972)
+++ trunk/pingus/src/string_format.hpp 2009-02-26 13:48:51 UTC (rev 3973)
@@ -24,14 +24,17 @@
class StringFormat {
public:
+ /** Joins series of whitespace into a single space, except doubte
+ newline which leaves a single newline (TeX style) */
+ static std::string normalize(std::string text);
+
/** Takes a string \a text and wraps it into multiple lines, each
less then \a width long. Line wrappings happens TeX style, i.e.
a double newline marks a newline, while other whitespace is
joined to a single space. */
- static std::string break_line(std::string text, int width, const Font& font);
+ static std::string break_line(const std::string& text, int width, const
Font& font);
};
-
#endif
/* EOF */
Modified: trunk/pingus/src/utf8_iterator.cpp
===================================================================
--- trunk/pingus/src/utf8_iterator.cpp 2009-02-26 12:33:31 UTC (rev 3972)
+++ trunk/pingus/src/utf8_iterator.cpp 2009-02-26 13:48:51 UTC (rev 3973)
@@ -91,54 +91,17 @@
return it;
}
-
-// FIXME: Get rid of exceptions in this code
-UTF8Iterator::UTF8Iterator(const std::string& text_)
- : text(text_),
- pos(0)
-{
- try {
- chr = decode_utf8(text, pos);
- } catch (std::exception) {
- std::cout << "Malformed utf-8 sequence beginning with " <<
*((uint32_t*)(text.c_str() + pos)) << " found " << std::endl;
- chr = 0;
- }
-}
-
-bool
-UTF8Iterator::done() const
-{
- return pos > text.size();
-}
-
-UTF8Iterator&
-UTF8Iterator::operator++() {
- try {
- chr = decode_utf8(text, pos);
- } catch (std::exception) {
- std::cout << "Malformed utf-8 sequence beginning with " <<
*((uint32_t*)(text.c_str() + pos)) << " found " << std::endl;
- chr = 0;
- ++pos;
- }
-
- return *this;
-}
-
-uint32_t
-UTF8Iterator::operator*() const {
- return chr;
-}
-
/**
* returns true if this byte matches a bitmask of 10xx.xxxx, i.e. it is the
2nd, 3rd or 4th byte of a multibyte utf8 string
*/
bool
-UTF8Iterator::has_multibyte_mark(unsigned char c) {
+UTF8::has_multibyte_mark(unsigned char c)
+{
return ((c & 0300) == 0200);
}
uint32_t
-UTF8Iterator::decode_utf8(const std::string& text)
+UTF8::decode_utf8(const std::string& text)
{
size_t p = 0;
return decode_utf8(text, p);
@@ -152,7 +115,7 @@
* See unicode standard section 3.10 table 3-5 and 3-6 for details.
*/
uint32_t
-UTF8Iterator::decode_utf8(const std::string& text, size_t& p)
+UTF8::decode_utf8(const std::string& text, size_t& p)
{
uint32_t c1 = (unsigned char) text[p+0];
@@ -196,6 +159,44 @@
throw std::runtime_error("Malformed utf-8 sequence");
}
+// FIXME: Get rid of exceptions in this code
+UTF8::iterator::iterator(const std::string& text_)
+ : text(text_),
+ pos(0)
+{
+ try {
+ chr = decode_utf8(text, pos);
+ } catch (std::exception) {
+ std::cout << "Malformed utf-8 sequence beginning with " <<
*((uint32_t*)(text.c_str() + pos)) << " found " << std::endl;
+ chr = 0;
+ }
+}
+
+bool
+UTF8::iterator::done() const
+{
+ return pos > text.size();
+}
+
+UTF8::iterator&
+UTF8::iterator::operator++() {
+ try {
+ chr = decode_utf8(text, pos);
+ } catch (std::exception) {
+ std::cout << "Malformed utf-8 sequence beginning with " <<
*((uint32_t*)(text.c_str() + pos)) << " found " << std::endl;
+ chr = 0;
+ ++pos;
+ }
+
+ return *this;
+}
+
+uint32_t
+UTF8::iterator::operator*() const
+{
+ return chr;
+}
+
#ifdef __TEST__
int main(int argc, char** argv)
{
Modified: trunk/pingus/src/utf8_iterator.hpp
===================================================================
--- trunk/pingus/src/utf8_iterator.hpp 2009-02-26 12:33:31 UTC (rev 3972)
+++ trunk/pingus/src/utf8_iterator.hpp 2009-02-26 13:48:51 UTC (rev 3973)
@@ -30,16 +30,7 @@
/** return true if a linebreak is allowed after this character */
static bool is_linebreak_character(uint32_t unicode);
-};
-
-class UTF8Iterator
-{
-private:
- const std::string& text;
- std::string::size_type pos;
- uint32_t chr;
-public:
/**
* returns true if this byte matches a bitmask of 10xx.xxxx, i.e. it is the
2nd, 3rd or 4th byte of a multibyte utf8 string
*/
@@ -55,12 +46,20 @@
static uint32_t decode_utf8(const std::string& text, size_t& p);
static uint32_t decode_utf8(const std::string& text);
+
+ class iterator
+ {
+ private:
+ const std::string& text;
+ std::string::size_type pos;
+ uint32_t chr;
-public:
- UTF8Iterator(const std::string& text_);
- bool done() const;
- UTF8Iterator& operator++();
- uint32_t operator*() const;
+ public:
+ iterator(const std::string& text_);
+ bool done() const;
+ iterator& operator++();
+ uint32_t operator*() const;
+ };
};
#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] r3973 - trunk/pingus/src,
grumbel at BerliOS <=