PageRenderTime 129ms CodeModel.GetById 107ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llui/lluistring.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 158 lines | 107 code | 23 blank | 28 comment | 8 complexity | 553ecbfa4e4b2710c4346f207f086e17 MD5 | raw file
  1/** 
  2 * @file lluistring.cpp
  3 * @brief LLUIString implementation.
  4 *
  5 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28#include "lluistring.h"
 29#include "llsd.h"
 30#include "lltrans.h"
 31
 32LLFastTimer::DeclareTimer FTM_UI_STRING("UI String");
 33
 34
 35LLUIString::LLUIString(const std::string& instring, const LLStringUtil::format_map_t& args)
 36:	mOrig(instring),
 37	mArgs(new LLStringUtil::format_map_t(args))
 38{
 39	dirty();
 40}
 41
 42void LLUIString::assign(const std::string& s)
 43{
 44	mOrig = s;
 45	dirty();
 46}
 47
 48void LLUIString::setArgList(const LLStringUtil::format_map_t& args)
 49
 50{
 51	getArgs() = args;
 52	dirty();
 53}
 54
 55void LLUIString::setArgs(const LLSD& sd)
 56{
 57	LLFastTimer timer(FTM_UI_STRING);
 58	
 59	if (!sd.isMap()) return;
 60	for(LLSD::map_const_iterator sd_it = sd.beginMap();
 61		sd_it != sd.endMap();
 62		++sd_it)
 63	{
 64		setArg(sd_it->first, sd_it->second.asString());
 65	}
 66	dirty();
 67}
 68
 69void LLUIString::setArg(const std::string& key, const std::string& replacement)
 70{
 71	getArgs()[key] = replacement;
 72	dirty();
 73}
 74
 75void LLUIString::truncate(S32 maxchars)
 76{
 77	if (getUpdatedWResult().size() > (size_t)maxchars)
 78	{
 79		LLWStringUtil::truncate(getUpdatedWResult(), maxchars);
 80		mResult = wstring_to_utf8str(getUpdatedWResult());
 81	}
 82}
 83
 84void LLUIString::erase(S32 charidx, S32 len)
 85{
 86	getUpdatedWResult().erase(charidx, len);
 87	mResult = wstring_to_utf8str(getUpdatedWResult());
 88}
 89
 90void LLUIString::insert(S32 charidx, const LLWString& wchars)
 91{
 92	getUpdatedWResult().insert(charidx, wchars);
 93	mResult = wstring_to_utf8str(getUpdatedWResult());
 94}
 95
 96void LLUIString::replace(S32 charidx, llwchar wc)
 97{
 98	getUpdatedWResult()[charidx] = wc;
 99	mResult = wstring_to_utf8str(getUpdatedWResult());
100}
101
102void LLUIString::clear()
103{
104	// Keep Args
105	mOrig.clear();
106	mResult.clear();
107	mWResult.clear();
108}
109
110void LLUIString::dirty()
111{
112	mNeedsResult = true;
113	mNeedsWResult = true;
114}
115
116void LLUIString::updateResult() const
117{
118	mNeedsResult = false;
119
120	LLFastTimer timer(FTM_UI_STRING);
121	
122	// optimize for empty strings (don't attempt string replacement)
123	if (mOrig.empty())
124	{
125		mResult.clear();
126		mWResult.clear();
127		return;
128	}
129	mResult = mOrig;
130	
131	// get the defailt args + local args
132	if (!mArgs || mArgs->empty())
133	{
134		LLStringUtil::format(mResult, LLTrans::getDefaultArgs());
135	}
136	else
137	{
138		LLStringUtil::format_map_t combined_args = LLTrans::getDefaultArgs();
139		combined_args.insert(mArgs->begin(), mArgs->end());
140		LLStringUtil::format(mResult, combined_args);
141	}
142}
143
144void LLUIString::updateWResult() const
145{
146	mNeedsWResult = false;
147
148	mWResult = utf8str_to_wstring(getUpdatedResult());
149}
150
151LLStringUtil::format_map_t& LLUIString::getArgs()
152{
153	if (!mArgs)
154	{
155		mArgs = new LLStringUtil::format_map_t;
156	}
157	return *mArgs;
158}