PageRenderTime 178ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  27. #include "lluistring.h"
  28. #include "llsd.h"
  29. #include "lltrans.h"
  30. LLFastTimer::DeclareTimer FTM_UI_STRING("UI String");
  31. LLUIString::LLUIString(const std::string& instring, const LLStringUtil::format_map_t& args)
  32. : mOrig(instring),
  33. mArgs(new LLStringUtil::format_map_t(args))
  34. {
  35. dirty();
  36. }
  37. void LLUIString::assign(const std::string& s)
  38. {
  39. mOrig = s;
  40. dirty();
  41. }
  42. void LLUIString::setArgList(const LLStringUtil::format_map_t& args)
  43. {
  44. getArgs() = args;
  45. dirty();
  46. }
  47. void LLUIString::setArgs(const LLSD& sd)
  48. {
  49. LLFastTimer timer(FTM_UI_STRING);
  50. if (!sd.isMap()) return;
  51. for(LLSD::map_const_iterator sd_it = sd.beginMap();
  52. sd_it != sd.endMap();
  53. ++sd_it)
  54. {
  55. setArg(sd_it->first, sd_it->second.asString());
  56. }
  57. dirty();
  58. }
  59. void LLUIString::setArg(const std::string& key, const std::string& replacement)
  60. {
  61. getArgs()[key] = replacement;
  62. dirty();
  63. }
  64. void LLUIString::truncate(S32 maxchars)
  65. {
  66. if (getUpdatedWResult().size() > (size_t)maxchars)
  67. {
  68. LLWStringUtil::truncate(getUpdatedWResult(), maxchars);
  69. mResult = wstring_to_utf8str(getUpdatedWResult());
  70. }
  71. }
  72. void LLUIString::erase(S32 charidx, S32 len)
  73. {
  74. getUpdatedWResult().erase(charidx, len);
  75. mResult = wstring_to_utf8str(getUpdatedWResult());
  76. }
  77. void LLUIString::insert(S32 charidx, const LLWString& wchars)
  78. {
  79. getUpdatedWResult().insert(charidx, wchars);
  80. mResult = wstring_to_utf8str(getUpdatedWResult());
  81. }
  82. void LLUIString::replace(S32 charidx, llwchar wc)
  83. {
  84. getUpdatedWResult()[charidx] = wc;
  85. mResult = wstring_to_utf8str(getUpdatedWResult());
  86. }
  87. void LLUIString::clear()
  88. {
  89. // Keep Args
  90. mOrig.clear();
  91. mResult.clear();
  92. mWResult.clear();
  93. }
  94. void LLUIString::dirty()
  95. {
  96. mNeedsResult = true;
  97. mNeedsWResult = true;
  98. }
  99. void LLUIString::updateResult() const
  100. {
  101. mNeedsResult = false;
  102. LLFastTimer timer(FTM_UI_STRING);
  103. // optimize for empty strings (don't attempt string replacement)
  104. if (mOrig.empty())
  105. {
  106. mResult.clear();
  107. mWResult.clear();
  108. return;
  109. }
  110. mResult = mOrig;
  111. // get the defailt args + local args
  112. if (!mArgs || mArgs->empty())
  113. {
  114. LLStringUtil::format(mResult, LLTrans::getDefaultArgs());
  115. }
  116. else
  117. {
  118. LLStringUtil::format_map_t combined_args = LLTrans::getDefaultArgs();
  119. combined_args.insert(mArgs->begin(), mArgs->end());
  120. LLStringUtil::format(mResult, combined_args);
  121. }
  122. }
  123. void LLUIString::updateWResult() const
  124. {
  125. mNeedsWResult = false;
  126. mWResult = utf8str_to_wstring(getUpdatedResult());
  127. }
  128. LLStringUtil::format_map_t& LLUIString::getArgs()
  129. {
  130. if (!mArgs)
  131. {
  132. mArgs = new LLStringUtil::format_map_t;
  133. }
  134. return *mArgs;
  135. }