removed not strictly needed error checking code

This commit is contained in:
dockes 2006-11-17 12:31:34 +00:00
parent a963035b93
commit df286f0df9

View File

@ -16,7 +16,7 @@
*/ */
#ifndef _UTF8ITER_H_INCLUDED_ #ifndef _UTF8ITER_H_INCLUDED_
#define _UTF8ITER_H_INCLUDED_ #define _UTF8ITER_H_INCLUDED_
/* @(#$Id: utf8iter.h,v 1.6 2006-01-30 11:15:28 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: utf8iter.h,v 1.7 2006-11-17 12:31:34 dockes Exp $ (C) 2004 J.F.Dockes */
/** /**
* A small helper class to iterate over utf8 strings. This is not an * A small helper class to iterate over utf8 strings. This is not an
@ -27,7 +27,6 @@ class Utf8Iter {
unsigned int cl; // Char length at current position if known unsigned int cl; // Char length at current position if known
const string &s; // String we're working with const string &s; // String we're working with
string::size_type pos; // Current position in string string::size_type pos; // Current position in string
bool bad; // Status
unsigned int m_charpos; // Current character posiiton unsigned int m_charpos; // Current character posiiton
// Get character byte length at specified position // Get character byte length at specified position
@ -55,11 +54,8 @@ class Utf8Iter {
// Update current char length in object state. Assumes pos is inside string // Update current char length in object state. Assumes pos is inside string
inline int compute_cl() { inline int compute_cl() {
cl = 0; cl = 0;
if (bad)
return -1;
cl = get_cl(pos); cl = get_cl(pos);
if (!poslok(pos, cl)) { if (!poslok(pos, cl)) {
bad = true;
pos = s.length(); pos = s.length();
cl = 0; cl = 0;
return -1; return -1;
@ -96,10 +92,14 @@ class Utf8Iter {
} }
public: public:
Utf8Iter(const string &in) Utf8Iter(const string &in)
: cl(0), s(in), pos(0), bad(false), m_charpos(0) {} : cl(0), s(in), pos(0), m_charpos(0)
{
// Ensure state is ok if appendchartostring is called at once
compute_cl();
}
void rewind() { void rewind() {
cl=0; pos=0; bad=false; m_charpos=0; cl=0; pos=0; m_charpos=0;
} }
/** operator* returns the ucs4 value as a machine integer*/ /** operator* returns the ucs4 value as a machine integer*/
unsigned int operator*() { unsigned int operator*() {
@ -107,7 +107,6 @@ class Utf8Iter {
return (unsigned int)-1; return (unsigned int)-1;
unsigned int val = getvalueat(pos, cl); unsigned int val = getvalueat(pos, cl);
if (val == (unsigned int)-1) { if (val == (unsigned int)-1) {
bad = true;
pos = s.length(); pos = s.length();
cl = 0; cl = 0;
} }
@ -137,7 +136,7 @@ class Utf8Iter {
/** Set current position before next utf-8 character */ /** Set current position before next utf-8 character */
string::size_type operator++(int) { string::size_type operator++(int) {
if (bad || (!cl && compute_cl() < 0)) { if (!cl && compute_cl() < 0) {
return pos = string::npos; return pos = string::npos;
} }
pos += cl; pos += cl;
@ -145,16 +144,12 @@ class Utf8Iter {
cl = 0; cl = 0;
return pos; return pos;
} }
/** This needs to be fast. No error checking. */
bool appendchartostring(string &out) { void appendchartostring(string &out) {
if (bad || (!cl && compute_cl() < 0)) { out.append(&s[pos], cl);
return false;
}
out += s.substr(pos, cl);
return true;
} }
operator string() { operator string() {
if (bad || (!cl && compute_cl() < 0)) { if (!cl && compute_cl() < 0) {
return std::string(""); return std::string("");
} }
return s.substr(pos, cl); return s.substr(pos, cl);
@ -165,7 +160,7 @@ class Utf8Iter {
return pos == s.length(); return pos == s.length();
} }
bool error() { bool error() {
return bad; return compute_cl() < 0;
} }
string::size_type getBpos() const { string::size_type getBpos() const {
return pos; return pos;