From 2cf98b74fc52411dfe61eed649ae755fc1b25df8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lars=20Gullik=20Bj=C3=B8nnes?= Date: Tue, 9 Nov 1999 22:20:24 +0000 Subject: [PATCH] LSubstring and LRegex added, some small cleanup in FileInfo, audited most of the assertions in lyxstring. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@299 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/FileInfo.C | 99 +++++++++++----------- src/support/FileInfo.h | 37 ++++----- src/support/LRegex.C | 173 +++++++++++++++++++++++++++++++++++++++ src/support/LRegex.h | 74 +++++++++++++++++ src/support/LSubstring.C | 94 +++++++++++++++++++++ src/support/LSubstring.h | 62 ++++++++++++++ src/support/Makefile.am | 4 + src/support/lyxstring.C | 136 +++++++++++++++--------------- 8 files changed, 541 insertions(+), 138 deletions(-) create mode 100644 src/support/LRegex.C create mode 100644 src/support/LRegex.h create mode 100644 src/support/LSubstring.C create mode 100644 src/support/LSubstring.h diff --git a/src/support/FileInfo.C b/src/support/FileInfo.C index b7ac32c5ce..2e2bf1c7f3 100644 --- a/src/support/FileInfo.C +++ b/src/support/FileInfo.C @@ -5,9 +5,9 @@ * LyX, The Document Processor * * Copyright 1995 Matthias Ettrich - * Copyright 1995-1998 The LyX Team. + * Copyright 1995-1999 The LyX Team. * - * ======================================================*/ + * ====================================================== */ #include @@ -121,37 +121,30 @@ FileInfo::FileInfo(string const & path, bool link) FileInfo::FileInfo(int fildes) { init(); - status = fstat(fildes, buf); + status = fstat(fildes, &buf); if (status) err = errno; } -FileInfo::~FileInfo() -{ - delete[] buf; -} - - void FileInfo::init() { status = 0; err = NoErr; - buf = (struct stat*) new char[sizeof(struct stat)]; } void FileInfo::dostat(bool link) { if (link) { - status = lstat(fname.c_str(), buf); + status = lstat(fname.c_str(), &buf); } else { - status = stat(fname.c_str(), buf); + status = stat(fname.c_str(), &buf); } if (status) err = errno; } -FileInfo& FileInfo::newFile(string const &path, bool link) +FileInfo & FileInfo::newFile(string const & path, bool link) { fname = path; @@ -164,11 +157,11 @@ FileInfo& FileInfo::newFile(string const &path, bool link) } -FileInfo& FileInfo::newFile(int fildes) +FileInfo & FileInfo::newFile(int fildes) { status = 0; err = NoErr; - status = fstat(fildes, buf); + status = fstat(fildes, &buf); if (status) err = errno; return *this; } @@ -176,17 +169,17 @@ FileInfo& FileInfo::newFile(int fildes) char const * FileInfo::typeIndicator() const { - if (S_ISDIR(buf->st_mode)) return ("/"); + if (S_ISDIR(buf.st_mode)) return ("/"); #ifdef S_ISLNK - if (S_ISLNK(buf->st_mode)) return ("@"); + if (S_ISLNK(buf.st_mode)) return ("@"); #endif #ifdef S_ISFIFO - if (S_ISFIFO(buf->st_mode)) return ("|"); + if (S_ISFIFO(buf.st_mode)) return ("|"); #endif #ifdef S_ISSOCK - if (S_ISSOCK(buf->st_mode)) return ("="); + if (S_ISSOCK(buf.st_mode)) return ("="); #endif - if (S_ISREG(buf->st_mode) && (buf->st_mode & (S_IEXEC | S_IXGRP | S_IXOTH))) + if (S_ISREG(buf.st_mode) && (buf.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH))) return ("*"); return ""; } @@ -194,13 +187,13 @@ char const * FileInfo::typeIndicator() const mode_t FileInfo::getMode() const { - return buf->st_mode; + return buf.st_mode; } long FileInfo::getBlockSize() const { #ifndef __EMX__ - return buf->st_blksize; /* Preferred I/O block size */ + return buf.st_blksize; /* Preferred I/O block size */ #else #warning May be fixed in 0.13 (SMiyata) return 512; /* Assume HPFS */ @@ -210,9 +203,9 @@ long FileInfo::getBlockSize() const void FileInfo::modeString(char * szString) const { szString[0] = typeLetter(); - flagRWX((buf->st_mode & 0700) << 0, &szString[1]); - flagRWX((buf->st_mode & 0070) << 3, &szString[4]); - flagRWX((buf->st_mode & 0007) << 6, &szString[7]); + flagRWX((buf.st_mode & 0700) << 0, &szString[1]); + flagRWX((buf.st_mode & 0070) << 3, &szString[4]); + flagRWX((buf.st_mode & 0007) << 6, &szString[7]); setSticky(szString); szString[10] = 0; } @@ -221,25 +214,25 @@ void FileInfo::modeString(char * szString) const char FileInfo::typeLetter() const { #ifdef S_ISBLK - if (S_ISBLK(buf->st_mode)) return 'b'; + if (S_ISBLK(buf.st_mode)) return 'b'; #endif - if (S_ISCHR(buf->st_mode)) return 'c'; - if (S_ISDIR(buf->st_mode)) return 'd'; - if (S_ISREG(buf->st_mode)) return '-'; + if (S_ISCHR(buf.st_mode)) return 'c'; + if (S_ISDIR(buf.st_mode)) return 'd'; + if (S_ISREG(buf.st_mode)) return '-'; #ifdef S_ISFIFO - if (S_ISFIFO(buf->st_mode)) return 'p'; + if (S_ISFIFO(buf.st_mode)) return 'p'; #endif #ifdef S_ISLNK - if (S_ISLNK(buf->st_mode)) return 'l'; + if (S_ISLNK(buf.st_mode)) return 'l'; #endif #ifdef S_ISSOCK - if (S_ISSOCK(buf->st_mode)) return 's'; + if (S_ISSOCK(buf.st_mode)) return 's'; #endif #ifdef S_ISMPC - if (S_ISMPC(buf->st_mode)) return 'm'; + if (S_ISMPC(buf.st_mode)) return 'm'; #endif #ifdef S_ISNWK - if (S_ISNWK(buf->st_mode)) return 'n'; + if (S_ISNWK(buf.st_mode)) return 'n'; #endif return '?'; } @@ -256,19 +249,19 @@ void FileInfo::flagRWX(unsigned short i, char * szString) const void FileInfo::setSticky(char * szString) const { #ifdef S_ISUID - if (buf->st_mode & S_ISUID) { + if (buf.st_mode & S_ISUID) { if (szString[3] != 'x') szString[3] = 'S'; else szString[3] = 's'; } #endif #ifdef S_ISGID - if (buf->st_mode & S_ISGID) { + if (buf.st_mode & S_ISGID) { if (szString[6] != 'x') szString[6] = 'S'; else szString[6] = 's'; } #endif #ifdef S_ISVTX - if (buf->st_mode & S_ISVTX) { + if (buf.st_mode & S_ISVTX) { if (szString[9] != 'x') szString[9] = 'T'; else szString[9] = 't'; } @@ -278,43 +271,43 @@ void FileInfo::setSticky(char * szString) const time_t FileInfo::getModificationTime() const { - return buf->st_mtime; + return buf.st_mtime; } time_t FileInfo::getAccessTime() const { - return buf->st_atime; + return buf.st_atime; } time_t FileInfo::getStatusChangeTime() const { - return buf->st_ctime; + return buf.st_ctime; } nlink_t FileInfo::getNumberOfLinks() const { - return buf->st_nlink; + return buf.st_nlink; } -uid_t FileInfo::getUid() const +uid_t FileInfo::getUid() const { - return buf->st_uid; + return buf.st_uid; } -gid_t FileInfo::getGid() const +gid_t FileInfo::getGid() const { - return buf->st_gid; + return buf.st_gid; } off_t FileInfo::getSize() const { - return buf->st_size; + return buf.st_size; } @@ -335,44 +328,44 @@ bool FileInfo::isOK() const bool FileInfo::isLink() const { - return S_ISLNK(buf->st_mode); + return S_ISLNK(buf.st_mode); } bool FileInfo::isRegular() const { - return S_ISREG(buf->st_mode); + return S_ISREG(buf.st_mode); } bool FileInfo::isDir() const { - return S_ISDIR(buf->st_mode); + return S_ISDIR(buf.st_mode); } bool FileInfo::isChar() const { - return S_ISCHR(buf->st_mode); + return S_ISCHR(buf.st_mode); } bool FileInfo::isBlock() const { - return S_ISBLK(buf->st_mode); + return S_ISBLK(buf.st_mode); } bool FileInfo::isFifo() const { - return S_ISFIFO(buf->st_mode); + return S_ISFIFO(buf.st_mode); } bool FileInfo::isSocket() const { #ifdef S_ISSOCK - return S_ISSOCK(buf->st_mode); + return S_ISSOCK(buf.st_mode); #else return false; #endif diff --git a/src/support/FileInfo.h b/src/support/FileInfo.h index 4eeea35899..9093601b1c 100644 --- a/src/support/FileInfo.h +++ b/src/support/FileInfo.h @@ -25,27 +25,24 @@ class FileInfo { public: /// FileInfo(); - + /** Get information about file. - If link is true, the information is about the link itself, not - the file that is obtained by tracing the links. */ + If link is true, the information is about the link itself, not + the file that is obtained by tracing the links. */ FileInfo(string const & path, bool link = false); /// File descriptor FileInfo(int fildes); - /// - ~FileInfo(); - /// Query a new file - FileInfo& newFile(string const & path, bool link = false); - + FileInfo & newFile(string const & path, bool link = false); + /// Query a new file descriptor - FileInfo& newFile(int fildes); + FileInfo & newFile(int fildes); /// Returns a character describing file type (ls -F) char const * typeIndicator() const; - + /// File protection mode mode_t getMode() const; @@ -63,31 +60,31 @@ public: /// updates mode string to match suid/sgid/sticky bits void setSticky(char * szString) const; - + /// time_t getModificationTime() const; - + /// time_t getAccessTime() const; - + /// time_t getStatusChangeTime() const; - + /// Total file size in bytes off_t getSize() const; - + /// Number of hard links nlink_t getNumberOfLinks() const; - + /// User ID of owner uid_t getUid() const; - + /// Group ID of owner gid_t getGid() const; - + /// Is the file information correct? Did the query succeed? bool isOK() const; - + /// Permission flags enum perm_test { rperm = R_OK, // test for read permission @@ -132,7 +129,7 @@ private: /// void dostat(bool); /// - struct stat * buf; + struct stat buf; /// int status; /// diff --git a/src/support/LRegex.C b/src/support/LRegex.C new file mode 100644 index 0000000000..6f7af728ac --- /dev/null +++ b/src/support/LRegex.C @@ -0,0 +1,173 @@ +#ifdef __GNUG__ +#pragma implementation +#endif + +#include + +#include +#include +#include "LRegex.h" + +/// +struct LRegex::Impl { + /// + re_pattern_buffer * preg; + + /// + int error_code; + + /// + mutable LRegex::SubMatches matches; + + /// + Impl(string const & regex) + : preg(new re_pattern_buffer), error_code(0) + { + error_code = regcomp(preg, regex.c_str(), REG_EXTENDED); + } + + /// + ~Impl() + { + regfree(preg); + delete preg; + } + + /// + bool exact_match(string const & str) const + { + regmatch_t tmp; + if (!regexec(preg, str.c_str(), 1, &tmp, 0)) { + if (tmp.rm_so == 0 && + tmp.rm_eo == static_cast(str.length())) + return true; + } + // no match + return false; + } + + /// + LRegex::MatchPair first_match(string const & str) const + { + regmatch_t tmp; + regexec(preg, str.c_str(), 1, &tmp, 0); + unsigned int first = tmp.rm_so != -1 ? + static_cast(tmp.rm_so) : string::npos; + unsigned int second = tmp.rm_eo != -1 ? + static_cast(tmp.rm_eo) : string::npos; + return make_pair(first, second - first); + } + + /// + string getError() const + { + size_t nr = regerror(error_code, preg, 0, 0); + char * tmp = new char[nr]; + regerror(error_code, preg, tmp, nr); + string ret(tmp); + delete [] tmp; + return ret; + } + + /// + LRegex::SubMatches const & exec(string const & str) const + { + // Some room for improvement in this func. I realize + // that it is double as expensive as needed, but that + // is something I am willing to pay to get the nice + // interface. One thing that can be done is to only put + // valid submatches into matches. That will not make this + // func much faster, but client code will be simpler, + // because then it will only be needed to scan through + // all the entries in matches. + size_t subs = (preg->re_nsub != 0 ? (preg->re_nsub + 1) : 1); + regmatch_t * mat = new regmatch_t[subs]; + unsigned int first = 0; + unsigned int second = 0; + matches.erase(matches.begin(), matches.end()); + if (!regexec(preg, str.c_str(), subs, mat, 0)) { // some match + matches.reserve(subs); + for (size_t i = 0; i < subs; ++i) { + first = mat[i].rm_so != -1 ? + static_cast + (mat[i].rm_so) : string::npos; + second = mat[i].rm_eo != -1 ? + static_cast + (mat[i].rm_eo) : string::npos; + matches.push_back(make_pair(first, + second - first)); + } + } + delete[] mat; + return matches; + } +}; + + +LRegex::LRegex(string const & regex) + : impl(new Impl(regex)) {} + + +LRegex::~LRegex() +{ + delete impl; +} + + +LRegex::SubMatches const & LRegex::exec(string const & str) const +{ + return impl->exec(str); +} + + +bool LRegex::exact_match(string const & str) const +{ + return impl->exact_match(str); +} + + +LRegex::MatchPair LRegex::first_match(string const & str) const +{ + return impl->first_match(str); +} + + +string LRegex::getError() const +{ + return impl->getError(); +} + + +int LRegex::getErrorCode() const +{ + return impl->error_code; +} + + +bool LRegex::ok() const { + return impl->error_code == 0; +} + + +#if 0 +// some built in regular expressions + +// this is good +const LRegex LRXwhite("[ \n\t\r\v\f]+"); +// this is good +const LRegex LRXint("-?[0-9]+"); +// this is good +const LRegex LRXdouble("-?(([0-9]+.[0-9]*)|" + "([0-9]+)|(.[0-9]+))" + "([eE][---+]?[0-9]+)?"); +// not usable +// const LRegex LRXalpha("[A-Za-z]+"); +// not usable (only ascii) +// const LRegex LRXlowercase("[a-z]+"); +// not usable (only ascii) +// const LRegex LRXuppercase("[A-Z]+"); +// not usable (only ascii) +// const LRegex LRXalphanum("[0-9A-Za-z]+"); +// this is good +const LRegex LRXidentifier("[A-Za-z_][A-Za-z0-9_]*"); +#endif diff --git a/src/support/LRegex.h b/src/support/LRegex.h new file mode 100644 index 0000000000..ebafc85402 --- /dev/null +++ b/src/support/LRegex.h @@ -0,0 +1,74 @@ +// -*- C++ -*- + +/* C++ wrapper around the POSIX regex functions: + regcomp, regexec, regerror, regfree. +*/ + +#ifndef LREGEX_H +#define LREGEX_H + +#ifdef __GNUG__ +#pragma interface +#endif + +#include + +#include "LString.h" + +/// +class LRegex { +public: + /// + LRegex(string const & regex); + + /// + ~LRegex(); + + /// + typedef pair MatchPair; + + /// + typedef vector SubMatches; + + /// Returns all the matches in a vector + SubMatches const & exec(string const & str) const; + + /// The whole of str matches regex. + bool exact_match(string const & str) const; + + /// + MatchPair first_match(string const & str) const; + + /// + string getError() const; + + /// + int getErrorCode() const; + + /// Will the next operation fail of not. + bool ok() const; +private: + /// + struct Impl; + + /// + Impl * impl; +}; + + +// We comment out these, we can comment them in when we need them. +#if 0 +// some built in regular expressions + +extern const LRegex LRXwhite; // = "[ \n\t\r\v\f]+" +extern const LRegex LRXint; // = "-?[0-9]+" +extern const LRegex LRXdouble; // = "-?\\(\\([0-9]+\\.[0-9]*\\)\\| + // \\([0-9]+\\)\\|\\(\\.[0-9]+\\)\\) + // \\([eE][---+]?[0-9]+\\)?" +//extern const LRegex LRXalpha; // = "[A-Za-z]+" +//extern const LRegex LRXlowercase; // = "[a-z]+" +//extern const LRegex LRXuppercase; // = "[A-Z]+" +//extern const LRegex LRXalphanum; // = "[0-9A-Za-z]+" +extern const LRegex LRXidentifier; // = "[A-Za-z_][A-Za-z0-9_]*" +#endif +#endif diff --git a/src/support/LSubstring.C b/src/support/LSubstring.C new file mode 100644 index 0000000000..bff2fe8c0c --- /dev/null +++ b/src/support/LSubstring.C @@ -0,0 +1,94 @@ +/* This file is part of + * ====================================================== + * + * LyX, The Document Processor + * + * Copyright (C) 1995 Matthias Ettrich + * Copyright (C) 1995-1998 The LyX Team. + * + *======================================================*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "LSubstring.h" + + +LSubstring::LSubstring(string & s, size_type i, size_type l) + : ps(&s), pos(i), n(l) +{ +} + + +LSubstring::LSubstring(string & s, string & s2) + : ps(&s), n(s2.length()) +{ + pos = s.find(s2); +} + + +LSubstring::LSubstring(string & s, char * p) + : ps(&s) +{ + n = strlen(p); + pos = s.find(p); +} + + +LSubstring::LSubstring(string & s, LRegex & r) + : ps(&s) +{ + LRegex::MatchPair res = r.first_match(s); + if (res.first != string::npos) { + n = res.second; + pos = res.first; + } else { + n = 0; + pos = 0; + } +} + + +LSubstring & LSubstring::operator=(string const & s) +{ + ps->replace(pos, n, s); // write through to *ps + return *this; +} + + +LSubstring & LSubstring::operator=(LSubstring const & s) +{ + ps->replace(pos, n, string(s, 0, string::npos)); + return *this; +} + + +LSubstring & LSubstring::operator=(char const * p) +{ + ps->replace(pos, n, p); + return *this; +} + + +LSubstring & LSubstring::operator=(char c) +{ + ps->replace(pos, n, 1, c); + return *this; +} + + +LSubstring::operator string() const +{ + return string(*ps, pos, n); // copy from *ps +} + + +#if 0 +LSubstring::operator char const * () const +{ + static string tr; + tr.assign(*ps, pos, n); + return tr.c_str(); +} +#endif diff --git a/src/support/LSubstring.h b/src/support/LSubstring.h new file mode 100644 index 0000000000..44463d3226 --- /dev/null +++ b/src/support/LSubstring.h @@ -0,0 +1,62 @@ +// -*- C++ -*- +/* This file is part of + * ====================================================== + * + * LyX, The Document Processor + * + * Copyright (C) 1995 Matthias Ettrich + * Copyright (C) 1995-1998 The LyX Team. + * + *======================================================*/ + +// This one is heavily based on the substring class in The C++ +// Programming Language by Bjarne Stroustrup + +#ifndef LSUBSTRING_H +#define LSUBSTRING_H + +#ifdef __GNUG__ +#pragma interface +#endif + + +#include "LString.h" +#include "LRegex.h" + +/// +class LSubstring { +public: + /// + typedef string::size_type size_type; + /// + LSubstring(string & s, size_type i, size_type n); + /// + LSubstring(string & s, string & s2); + /// + LSubstring(string & s, string::value_type * p); + /// + LSubstring(string & s, LRegex & r); + /// + LSubstring & operator=(string const &); + /// + LSubstring & operator=(LSubstring const &); + /// + LSubstring & operator=(string::value_type const *); + /// + LSubstring & operator=(string::value_type); + /// + operator string() const; +#if 0 + /// + operator char const * () const; +#endif +private: + /// + string * ps; + /// + size_type pos; + /// + size_type n; +}; + +#endif diff --git a/src/support/Makefile.am b/src/support/Makefile.am index d52063fe80..3b13ab6e9a 100644 --- a/src/support/Makefile.am +++ b/src/support/Makefile.am @@ -16,6 +16,10 @@ libsupport_a_SOURCES = \ LAssert.h \ LIstream.h \ LOstream.h \ + LRegex.C \ + LRegex.h \ + LSubstring.C \ + LSubstring.h \ filetools.C \ filetools.h \ lstrings.C \ diff --git a/src/support/lyxstring.C b/src/support/lyxstring.C index 65d07060fa..9b6d24ea0f 100644 --- a/src/support/lyxstring.C +++ b/src/support/lyxstring.C @@ -31,6 +31,8 @@ using std::min; // in this file consult me and/or the standard to discover the // right behavior. +// Asserts with a STD! are required by the standard. +// Asserts with a OURS! are added by me. // Reference count has been checked, empty_rep removed and // introduced again in a similar guise. Where is empty_rep _really_ // needed? @@ -42,6 +44,7 @@ using std::min; // I have so far not tested them extensively and would be // happy if others took the time to have a peek. +// Lgb. /////////////////////////////////////// // The internal string representation @@ -207,7 +210,6 @@ void lyxstring::Srep::push_back(value_type c) void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p, lyxstring::size_type n) { - Assert(pos <= sz); if (res < n + sz) { res = sz + n + xtra; value_type * tmp = new value_type[res + 1]; @@ -227,7 +229,6 @@ void lyxstring::Srep::insert(lyxstring::size_type pos, const value_type * p, void lyxstring::Srep::resize(size_type n, value_type c) { - Assert(n < npos); // This resets sz to res_arg res = min(n, npos - 2); // We keep no xtra when we resize value_type * tmp = new value_type[res + 1]; @@ -256,7 +257,6 @@ void lyxstring::Srep::replace(lyxstring::size_type i, lyxstring::size_type n, value_type const * p, size_type n2) { // can be called with p=0 and n2=0 - Assert(i < sz && ((!p && !n2) || p)); n = min(sz - i, n); sz -= n; if (res >= n2 + sz) { @@ -376,7 +376,7 @@ lyxstring::lyxstring() lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n) { - Assert(pos < x.rep->sz || pos == 0); + Assert(pos <= x.rep->sz); // STD! if (pos == 0 && n >= x.length()) { // this is the default x.rep->ref++; rep = x.rep; @@ -388,7 +388,7 @@ lyxstring::lyxstring(lyxstring const & x, size_type pos, size_type n) lyxstring::lyxstring(value_type const * s, size_type n) { - Assert(s); // we don't allow null pointers + Assert(s && n < npos); // STD! static Srep empty_rep(0, ""); if (*s && n) { // s is not empty string and n > 0 rep = new Srep(min(strlen(s), n), s); @@ -401,7 +401,7 @@ lyxstring::lyxstring(value_type const * s, size_type n) lyxstring::lyxstring(value_type const * s) { - Assert(s); // we don't allow null pointers + Assert(s); // STD! static Srep empty_rep(0, ""); if (*s) { // s is not empty string rep = new Srep(strlen(s), s); @@ -414,6 +414,7 @@ lyxstring::lyxstring(value_type const * s) lyxstring::lyxstring(size_type n, value_type c) { + Assert(n < npos); // STD! rep = new Srep(n, c); } @@ -494,6 +495,7 @@ lyxstring::size_type lyxstring::size() const void lyxstring::resize(size_type n, value_type c) { + Assert(n <= npos); // STD! TestlyxstringInvariant(this); // This resets sz to res_arg @@ -531,7 +533,7 @@ lyxstring & lyxstring::operator=(lyxstring const & x) lyxstring & lyxstring::operator=(value_type const * s) { - Assert(s); + Assert(s); // OURS! TestlyxstringInvariant(this); // printf("lyxstring::operator=(value_type const *)\n"); @@ -568,6 +570,7 @@ lyxstring & lyxstring::assign(lyxstring const & x) lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n) { + Assert(pos <= x.rep->sz); // STD! TestlyxstringInvariant(this); return assign(x.substr(pos, n)); @@ -576,7 +579,7 @@ lyxstring & lyxstring::assign(lyxstring const & x, size_type pos, size_type n) lyxstring & lyxstring::assign(value_type const * s, size_type n) { - Assert(s); + Assert(s); // OURS! TestlyxstringInvariant(this); n = min(strlen(s), n); @@ -592,7 +595,7 @@ lyxstring & lyxstring::assign(value_type const * s, size_type n) lyxstring & lyxstring::assign(value_type const * s) { - Assert(s); + Assert(s); // OURS! TestlyxstringInvariant(this); return assign(s, strlen(s)); @@ -625,14 +628,14 @@ lyxstring & lyxstring::assign(iterator first, iterator last) lyxstring::const_reference lyxstring::operator[](size_type pos) const { - Assert(pos < rep->sz); - return rep->s[pos]; + Assert(pos <= rep->sz); // OURS! + return pos == rep->sz ? '\0' : rep->s[pos]; } lyxstring::reference lyxstring::operator[](size_type pos) { - Assert(pos < rep->sz); + Assert(pos < rep->sz); // OURS! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -642,14 +645,14 @@ lyxstring::reference lyxstring::operator[](size_type pos) lyxstring::const_reference lyxstring::at(size_type n) const { - Assert(n < rep->sz); + Assert(n < rep->sz); // STD! return rep->s[n]; } lyxstring::reference lyxstring::at(size_type n) { - Assert(n < rep->sz); + Assert(n < rep->sz); // STD! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -671,7 +674,7 @@ lyxstring & lyxstring::operator+=(lyxstring const & x) lyxstring & lyxstring::operator+=(value_type const * x) { - Assert(x); + Assert(x); // OURS! TestlyxstringInvariant(this); return append(x); @@ -709,6 +712,7 @@ lyxstring & lyxstring::append(lyxstring const & x) lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n) { + Assert(pos <= x.rep->sz); // STD! TestlyxstringInvariant(this); return append(x.substr(pos, n)); @@ -717,7 +721,7 @@ lyxstring & lyxstring::append(lyxstring const & x, size_type pos, size_type n) lyxstring & lyxstring::append(value_type const * p, size_type n) { - Assert(p); + Assert(p); // OURS! TestlyxstringInvariant(this); if (!*p || !n) return *this; @@ -729,7 +733,7 @@ lyxstring & lyxstring::append(value_type const * p, size_type n) lyxstring & lyxstring::append(value_type const * p) { - Assert(p); + Assert(p); // OURS! TestlyxstringInvariant(this); if (!*p) return *this; @@ -774,6 +778,7 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x) lyxstring & lyxstring::insert(size_type pos, lyxstring const & x, size_type pos2, size_type n) { + Assert(pos <= rep->sz && pos2 <= x.rep->sz); // STD! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -784,7 +789,7 @@ lyxstring & lyxstring::insert(size_type pos, lyxstring const & x, lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n) { - Assert(p); + Assert(p); // OURS! TestlyxstringInvariant(this); if (*p && n) { @@ -798,7 +803,7 @@ lyxstring & lyxstring::insert(size_type pos, value_type const * p, size_type n) lyxstring & lyxstring::insert(size_type pos, value_type const * p) { - Assert(p); + Assert(p); // OURS! TestlyxstringInvariant(this); if (*p) { @@ -886,7 +891,7 @@ lyxstring::size_type lyxstring::find(lyxstring const & a, size_type i) const lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i, size_type n) const { - Assert(ptr); + Assert(ptr); // OURS! if (!rep->sz || !*ptr || i >= rep->sz) return npos; TestlyxstringInvariant(this); @@ -916,7 +921,7 @@ lyxstring::size_type lyxstring::find(value_type const * ptr, size_type i, lyxstring::size_type lyxstring::find(value_type const * s, size_type i) const { - Assert(s); + Assert(s); // OURS! if (!rep->sz || i >= rep->sz) return npos; TestlyxstringInvariant(this); @@ -961,7 +966,7 @@ lyxstring::size_type lyxstring::rfind(lyxstring const & a, size_type i) const lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i, size_type n) const { - Assert(ptr); + Assert(ptr); // OURS! TestlyxstringInvariant(this); if (!*ptr) return npos; @@ -983,7 +988,7 @@ lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i, lyxstring::size_type lyxstring::rfind(value_type const * ptr, size_type i) const { - Assert(ptr); + Assert(ptr); // OURS! TestlyxstringInvariant(this); if (!*ptr) return npos; @@ -1017,7 +1022,7 @@ lyxstring::size_type lyxstring::rfind(value_type c, size_type i) const lyxstring::size_type lyxstring::find_first_of(lyxstring const & a, size_type i) const { - Assert(i < rep->sz); + Assert(i < rep->sz); // OURS! TestlyxstringInvariant(this); for (size_type t = i; t < rep->sz; ++t) { @@ -1030,7 +1035,7 @@ lyxstring::size_type lyxstring::find_first_of(lyxstring const & a, lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i, size_type n) const { - Assert(ptr && i < rep->sz); + Assert(ptr && i < rep->sz); // OURS! TestlyxstringInvariant(this); if (!n) return npos; @@ -1044,7 +1049,7 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, size_type i) const { - Assert(ptr && i < rep->sz); + Assert(ptr && i < rep->sz); // OURS! TestlyxstringInvariant(this); for (size_type t = i; t < rep->sz; ++t) { @@ -1056,7 +1061,7 @@ lyxstring::size_type lyxstring::find_first_of(value_type const * ptr, lyxstring::size_type lyxstring::find_first_of(value_type c, size_type i) const { - Assert(i < rep->sz); + Assert(i < rep->sz); // OURS! TestlyxstringInvariant(this); for (size_type t = i; t < rep->sz; ++t) { @@ -1081,7 +1086,7 @@ lyxstring::size_type lyxstring::find_last_of(lyxstring const & a, lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i, size_type n) const { - Assert(ptr); + Assert(ptr); // OURS! TestlyxstringInvariant(this); if (!n) return npos; @@ -1096,7 +1101,7 @@ lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i lyxstring::size_type lyxstring::find_last_of(value_type const * ptr, size_type i) const { - Assert(ptr); + Assert(ptr); // OURS! TestlyxstringInvariant(this); size_type ii = min(rep->sz - 1, i); @@ -1138,7 +1143,7 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i, size_type n) const { - Assert(ptr && i < rep->sz); + Assert(ptr && i < rep->sz); // OURS! TestlyxstringInvariant(this); if (!n) return (i < rep->sz) ? i : npos; @@ -1152,7 +1157,7 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, lyxstring::size_type lyxstring::find_first_not_of(value_type const * ptr, size_type i) const { - Assert(ptr && i < rep->sz); + Assert(ptr && i < rep->sz); // OURS! TestlyxstringInvariant(this); for (size_type t = i; t < rep->sz; ++t) { @@ -1166,7 +1171,7 @@ lyxstring::size_type lyxstring::find_first_not_of(value_type c, size_type i) const { if (!rep->sz) return npos; - Assert(i < rep->sz); + Assert(i < rep->sz); // OURS! TestlyxstringInvariant(this); for (size_type t = i; t < rep->sz; ++t) { @@ -1193,7 +1198,7 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, size_type i, size_type n) const { - Assert(ptr); + Assert(ptr); // OURS! TestlyxstringInvariant(this); if (!n) return npos; @@ -1209,7 +1214,7 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, lyxstring::size_type lyxstring::find_last_not_of(value_type const * ptr, size_type i) const { - Assert(ptr); + Assert(ptr); // OURS! TestlyxstringInvariant(this); size_type ii = min(rep->sz - 1, i); @@ -1239,17 +1244,17 @@ lyxstring::size_type lyxstring::find_last_not_of(value_type c, lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x) { - Assert(i < rep->sz || i == 0); + Assert(i <= rep->sz); // OURS! TestlyxstringInvariant(this); - return replace(i, n, x, 0, x.length()); + return replace(i, n, x, 0, x.rep->sz); } -lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x, +lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring const & x, size_type i2, size_type n2) { - Assert((i < rep->sz || i == 0) && (i2 < x.rep->sz || i2 == 0)); + Assert(i <= rep->sz && i2 <= x.rep->sz); // STD! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -1261,7 +1266,7 @@ lyxstring & lyxstring::replace(size_type i,size_type n, lyxstring const & x, lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p, size_type n2) { - Assert(p && i < rep->sz); + Assert(p && i < rep->sz); // OURS! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -1272,7 +1277,7 @@ lyxstring & lyxstring::replace(size_type i, size_type n, lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p) { - Assert(p && i < rep->sz); + Assert(p && i < rep->sz); // OURS! TestlyxstringInvariant(this); return replace(i, min(n, rep->sz), p, (!p) ? 0 : strlen(p)); @@ -1282,7 +1287,7 @@ lyxstring & lyxstring::replace(size_type i, size_type n, value_type const * p) lyxstring & lyxstring::replace(size_type i, size_type n, size_type n2, value_type c) { - Assert(i < rep->sz); + Assert(i < rep->sz); // OURS! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -1305,7 +1310,7 @@ lyxstring & lyxstring::replace(iterator i, iterator i2, const lyxstring & str) lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p, size_type n) { - Assert(p); + Assert(p); // OURS! TestlyxstringInvariant(this); return replace(i - begin(), i2 - i, p, n); @@ -1314,7 +1319,7 @@ lyxstring & lyxstring::replace(iterator i, iterator i2, lyxstring & lyxstring::replace(iterator i, iterator i2, value_type const * p) { - Assert(p); + Assert(p); // OURS! TestlyxstringInvariant(this); return replace(i - begin(), i2 - i, p); @@ -1341,7 +1346,7 @@ lyxstring & lyxstring::replace(iterator i, iterator i2, lyxstring & lyxstring::erase(size_type i, size_type n) { - Assert(i < rep->sz || i == 0); + Assert(i <= rep->sz); // STD! TestlyxstringInvariant(this); rep = rep->get_own_copy(); @@ -1396,7 +1401,8 @@ lyxstring::value_type const * lyxstring::data() const lyxstring::size_type lyxstring::copy(value_type * buf, size_type len, size_type pos) const { - Assert(buf); + Assert(buf); // OURS! + Assert(pos <= rep->sz); // STD! TestlyxstringInvariant(this); register int nn = min(len, length() - pos); @@ -1444,7 +1450,7 @@ int lyxstring::compare(lyxstring const & str) const int lyxstring::compare(value_type const * s) const { - Assert(s); + Assert(s); //OURS! TestlyxstringInvariant(this); int n = (!s) ? 0 : strlen(s); return internal_compare(0, rep->sz, s, n, n); @@ -1453,7 +1459,7 @@ int lyxstring::compare(value_type const * s) const int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const { - Assert(pos < rep->sz || pos == 0); + Assert(pos <= rep->sz); // OURS! TestlyxstringInvariant(this); return internal_compare(pos, n, str.rep->s, str.rep->sz, str.rep->sz); } @@ -1462,8 +1468,8 @@ int lyxstring::compare(size_type pos, size_type n, lyxstring const & str) const int lyxstring::compare(size_type pos, size_type n, lyxstring const & str, size_type pos2, size_type n2) const { - Assert(pos < rep->sz || pos == 0); - Assert(pos2 < str.rep->sz || pos2 == 0); + Assert(pos <= rep->sz); // OURS! + Assert(pos2 <= str.rep->sz); // OURS! TestlyxstringInvariant(this); return internal_compare(pos, n, str.rep->s + pos2, @@ -1474,7 +1480,7 @@ int lyxstring::compare(size_type pos, size_type n, lyxstring const & str, int lyxstring::compare(size_type pos, size_type n, value_type const * s, size_type n2) const { - Assert(s && (pos < rep->sz || pos == 0)); + Assert(s && pos <= rep->sz); // OURS! TestlyxstringInvariant(this); return internal_compare(pos, n, s, (!s) ? 0 : strlen(s), n2); } @@ -1487,7 +1493,7 @@ int lyxstring::compare(size_type pos, size_type n, value_type const * s, // i = index, n = length lyxstring lyxstring::substr(size_type i, size_type n) const { - Assert(i < rep->sz || i == 0); + Assert(i <= rep->sz); // STD! TestlyxstringInvariant(this); return lyxstring(*this, i, n); @@ -1506,14 +1512,14 @@ bool operator==(lyxstring const & a, lyxstring const & b) bool operator==(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! return b.compare(a) == 0; } bool operator==(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! return a.compare(b) == 0; } @@ -1526,14 +1532,14 @@ bool operator!=(lyxstring const & a, lyxstring const & b) bool operator!=(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! return b.compare(a) != 0; } bool operator!=(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! return a.compare(b) != 0; } @@ -1546,14 +1552,14 @@ bool operator>(lyxstring const & a, lyxstring const & b) bool operator>(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! return b.compare(a) < 0; // since we reverse the parameters } bool operator>(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! return a.compare(b) > 0; } @@ -1566,14 +1572,14 @@ bool operator<(lyxstring const & a, lyxstring const & b) bool operator<(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! return b.compare(a) > 0; // since we reverse the parameters } bool operator<(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! return a.compare(b) < 0; } @@ -1586,14 +1592,14 @@ bool operator>=(lyxstring const & a, lyxstring const & b) bool operator>=(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! return b.compare(a) <= 0; // since we reverse the parameters } bool operator>=(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! return a.compare(b) >= 0; } @@ -1606,14 +1612,14 @@ bool operator<=(lyxstring const & a, lyxstring const & b) bool operator<=(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! return b.compare(a) >= 0; // since we reverse the parameters } bool operator<=(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! return a.compare(b) <= 0; } @@ -1628,7 +1634,7 @@ lyxstring operator+(lyxstring const & a, lyxstring const & b) lyxstring operator+(lyxstring::value_type const * a, lyxstring const & b) { - Assert(a); + Assert(a); // OURS! lyxstring tmp(a); tmp += b; return tmp; @@ -1646,7 +1652,7 @@ lyxstring operator+(lyxstring::value_type a, lyxstring const & b) lyxstring operator+(lyxstring const & a, lyxstring::value_type const * b) { - Assert(b); + Assert(b); // OURS! lyxstring tmp(a); tmp += b; return tmp; -- 2.39.2