X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FEncoding.cpp;h=9dde12d1213fc6d06e80ba0d28d27345605e1c39;hb=2de30c62f8d671a8c8d4d52a6a7310e2c5ca84de;hp=e4bbbc044abeccaa12d9eb9fbd3bcedd6115a2c0;hpb=d6337a248847aa4c72f8a813d92d349d1035c79f;p=lyx.git diff --git a/src/Encoding.cpp b/src/Encoding.cpp index e4bbbc044a..9dde12d121 100644 --- a/src/Encoding.cpp +++ b/src/Encoding.cpp @@ -19,13 +19,15 @@ #include "support/debug.h" #include "support/gettext.h" #include "support/lstrings.h" +#include "support/mutex.h" #include "support/textutils.h" #include "support/unicode.h" #include -#include +#include #include +#include using namespace std; using namespace lyx::support; @@ -73,15 +75,16 @@ const char * EncodingException::what() const throw() CharInfo::CharInfo( - docstring const textcommand, docstring const mathcommand, - std::string const textpreamble, std::string const mathpreamble, - std::string const tipashortcut, unsigned int flags) + docstring const & textcommand, docstring const & mathcommand, + std::string const & textpreamble, std::string const & mathpreamble, + std::string const & tipashortcut, unsigned int flags) : textcommand_(textcommand), mathcommand_(mathcommand), textpreamble_(textpreamble), mathpreamble_(mathpreamble), tipashortcut_(tipashortcut), flags_(flags) { } + Encoding::Encoding(string const & n, string const & l, string const & g, string const & i, bool f, bool u, Encoding::Package p) : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), @@ -104,10 +107,28 @@ Encoding::Encoding(string const & n, string const & l, string const & g, void Encoding::init() const { + // Since the the constructor is the only method which sets complete_ + // to false the test for complete_ is thread-safe without mutex. + if (complete_) + return; + + static Mutex mutex; + Mutex::Locker lock(&mutex); + + // We need to test again for complete_, since another thread could + // have set it to true while we were waiting for the lock and we must + // not modify an encoding which is already complete. if (complete_) return; - start_encodable_ = 0; + // We do not make any member mutable so that it can be easily verified + // that all const methods are thread-safe: init() is the only const + // method which changes complete_, encodable_ and start_encodable_, and + // it uses a mutex to ensure thread-safety. + CharSet & encodable = const_cast(this)->encodable_; + char_type & start_encodable = const_cast(this)->start_encodable_; + + start_encodable = 0; // temporarily switch off lyxerr, since we will generate iconv errors lyxerr.disable(); if (fixedwidth_) { @@ -121,10 +142,10 @@ void Encoding::init() const char_type const uc = ucs4[0]; CharInfoMap::const_iterator const it = unicodesymbols.find(uc); if (it == unicodesymbols.end()) - encodable_.insert(uc); + encodable.insert(uc); else if (!it->second.force()) { if (forced_->empty() || forced_->find(uc) == forced_->end()) - encodable_.insert(uc); + encodable.insert(uc); } } } else { @@ -137,22 +158,22 @@ void Encoding::init() const if (!eightbit.empty()) { CharInfoMap::const_iterator const it = unicodesymbols.find(c); if (it == unicodesymbols.end()) - encodable_.insert(c); + encodable.insert(c); else if (!it->second.force()) { if (forced_->empty() || forced_->find(c) == forced_->end()) - encodable_.insert(c); + encodable.insert(c); } } } } lyxerr.enable(); - CharSet::iterator it = encodable_.find(start_encodable_); - while (it != encodable_.end()) { - encodable_.erase(it); - ++start_encodable_; - it = encodable_.find(start_encodable_); + CharSet::iterator it = encodable.find(start_encodable); + while (it != encodable.end()) { + encodable.erase(it); + ++start_encodable; + it = encodable.find(start_encodable); } - complete_ = true; + const_cast(this)->complete_ = true; } @@ -196,7 +217,7 @@ pair Encoding::latexChar(char_type c) const } -pair Encoding::latexString(docstring const input, bool dryrun) const +pair Encoding::latexString(docstring const & input, bool dryrun) const { docstring result; docstring uncodable; @@ -239,16 +260,15 @@ vector Encoding::symbolsList() const // assure the used encoding is properly initialized init(); - // first all encodable characters - vector symbols(encodable_.begin(), encodable_.end()); - // add those below start_encodable_ + // first all those below start_encodable_ + vector symbols; for (char_type c = 0; c < start_encodable_; ++c) symbols.push_back(c); + //add all encodable characters + copy(encodable_.begin(), encodable_.end(), back_inserter(symbols)); // now the ones from the unicodesymbols file - CharInfoMap::const_iterator const end = unicodesymbols.end(); - CharInfoMap::const_iterator it = unicodesymbols.begin(); - for (; it != end; ++it) - symbols.push_back(it->first); + for (pair const & elem : unicodesymbols) + symbols.push_back(elem.first); return symbols; } @@ -297,6 +317,8 @@ char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype, CharInfoMap::const_iterator const end = unicodesymbols.end(); CharInfoMap::const_iterator it = unicodesymbols.begin(); for (combining = false; it != end; ++it) { + if (it->second.deprecated()) + continue; docstring const math = it->second.mathcommand(); docstring const text = it->second.textcommand(); if ((cmdtype & MATH_CMD) && math == cmd) { @@ -382,6 +404,8 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype, size_t unicmd_size = 0; char_type c = 0; for (; it != uniend; ++it) { + if (it->second.deprecated()) + continue; docstring const math = mathmode ? it->second.mathcommand() : docstring(); docstring const text = textmode ? it->second.textcommand() @@ -451,7 +475,7 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype, || (tmp.size() == prefix + 1 && !isAlphaASCII(tmp[1]) && (prefix == 1 || !isAlphaASCII(tmp[2]))) - || k == cmdend + || k == cmdend || !isAlphaASCII(cmd[k]) || tmp[tmp.size() - 1] == '}' ) { @@ -577,13 +601,25 @@ bool Encodings::isMathAlpha(char_type c) } +bool Encodings::isUnicodeTextOnly(char_type c) +{ + if (isASCII(c) || isMathAlpha(c)) + return false; + + CharInfoMap::const_iterator const it = unicodesymbols.find(c); + return it == unicodesymbols.end() || it->second.mathcommand().empty(); +} + + Encoding const * Encodings::fromLyXName(string const & name, bool allowUnsafe) const { EncodingList::const_iterator const it = encodinglist.find(name); + if (it == encodinglist.end()) + return 0; if (!allowUnsafe && it->second.unsafe()) return 0; - return it != encodinglist.end() ? &it->second : 0; + return &it->second; } @@ -660,7 +696,7 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile) if (!symbolslex.next(true)) break; string sflags = symbolslex.getString(); - + string tipashortcut; int flags = 0; @@ -700,6 +736,8 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile) flags &= ~CharInfoMathNoTermination; } else if (contains(flag, "tipashortcut=")) { tipashortcut = split(flag, '='); + } else if (flag == "deprecated") { + flags |= CharInfoDeprecated; } else { lyxerr << "Ignoring unknown flag `" << flag << "' for symbol `0x"