error_utils.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "extensions/common/error_utils.h"
  5. #include <initializer_list>
  6. #include <ostream>
  7. #include "base/check_op.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/strings/string_tokenizer.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. namespace extensions {
  13. namespace {
  14. std::string FormatErrorMessageInternal(
  15. base::StringPiece format,
  16. std::initializer_list<base::StringPiece> args) {
  17. std::string format_str(format);
  18. base::StringTokenizer tokenizer(format_str, "*");
  19. tokenizer.set_options(base::StringTokenizer::RETURN_DELIMS);
  20. std::vector<base::StringPiece> result_pieces;
  21. auto* args_it = args.begin();
  22. while (tokenizer.GetNext()) {
  23. if (!tokenizer.token_is_delim()) {
  24. result_pieces.push_back(tokenizer.token_piece());
  25. continue;
  26. }
  27. CHECK_NE(args_it, args.end())
  28. << "More placeholders (*) than substitutions.";
  29. // Substitute the argument.
  30. result_pieces.push_back(*args_it);
  31. args_it++;
  32. }
  33. // Not all substitutions were consumed.
  34. CHECK_EQ(args_it, args.end()) << "Fewer placeholders (*) than substitutions.";
  35. return base::JoinString(result_pieces, "" /* separator */);
  36. }
  37. } // namespace
  38. std::string ErrorUtils::FormatErrorMessage(base::StringPiece format,
  39. base::StringPiece s1) {
  40. return FormatErrorMessageInternal(format, {s1});
  41. }
  42. std::string ErrorUtils::FormatErrorMessage(base::StringPiece format,
  43. base::StringPiece s1,
  44. base::StringPiece s2) {
  45. return FormatErrorMessageInternal(format, {s1, s2});
  46. }
  47. std::string ErrorUtils::FormatErrorMessage(base::StringPiece format,
  48. base::StringPiece s1,
  49. base::StringPiece s2,
  50. base::StringPiece s3) {
  51. return FormatErrorMessageInternal(format, {s1, s2, s3});
  52. }
  53. std::u16string ErrorUtils::FormatErrorMessageUTF16(base::StringPiece format,
  54. base::StringPiece s1) {
  55. return base::UTF8ToUTF16(FormatErrorMessageInternal(format, {s1}));
  56. }
  57. std::u16string ErrorUtils::FormatErrorMessageUTF16(base::StringPiece format,
  58. base::StringPiece s1,
  59. base::StringPiece s2) {
  60. return base::UTF8ToUTF16(FormatErrorMessageInternal(format, {s1, s2}));
  61. }
  62. std::u16string ErrorUtils::FormatErrorMessageUTF16(base::StringPiece format,
  63. base::StringPiece s1,
  64. base::StringPiece s2,
  65. base::StringPiece s3) {
  66. return base::UTF8ToUTF16(FormatErrorMessageInternal(format, {s1, s2, s3}));
  67. }
  68. } // namespace extensions