summaryrefslogtreecommitdiff
path: root/chromium/extensions/common/error_utils.cc
blob: c75e4ce342b417d6d65dffe50183dde2fd0a767f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/common/error_utils.h"

#include <initializer_list>
#include <ostream>

#include "base/check_op.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"

namespace extensions {

namespace {

std::string FormatErrorMessageInternal(
    base::StringPiece format,
    std::initializer_list<base::StringPiece> args) {
  std::string format_str(format);
  base::StringTokenizer tokenizer(format_str, "*");
  tokenizer.set_options(base::StringTokenizer::RETURN_DELIMS);

  std::vector<base::StringPiece> result_pieces;
  auto* args_it = args.begin();
  while (tokenizer.GetNext()) {
    if (!tokenizer.token_is_delim()) {
      result_pieces.push_back(tokenizer.token_piece());
      continue;
    }

    CHECK_NE(args_it, args.end())
        << "More placeholders (*) than substitutions.";

    // Substitute the argument.
    result_pieces.push_back(*args_it);
    args_it++;
  }

  // Not all substitutions were consumed.
  CHECK_EQ(args_it, args.end()) << "Fewer placeholders (*) than substitutions.";

  return base::JoinString(result_pieces, "" /* separator */);
}

}  // namespace

std::string ErrorUtils::FormatErrorMessage(base::StringPiece format,
                                           base::StringPiece s1) {
  return FormatErrorMessageInternal(format, {s1});
}

std::string ErrorUtils::FormatErrorMessage(base::StringPiece format,
                                           base::StringPiece s1,
                                           base::StringPiece s2) {
  return FormatErrorMessageInternal(format, {s1, s2});
}

std::string ErrorUtils::FormatErrorMessage(base::StringPiece format,
                                           base::StringPiece s1,
                                           base::StringPiece s2,
                                           base::StringPiece s3) {
  return FormatErrorMessageInternal(format, {s1, s2, s3});
}

std::u16string ErrorUtils::FormatErrorMessageUTF16(base::StringPiece format,
                                                   base::StringPiece s1) {
  return base::UTF8ToUTF16(FormatErrorMessageInternal(format, {s1}));
}

std::u16string ErrorUtils::FormatErrorMessageUTF16(base::StringPiece format,
                                                   base::StringPiece s1,
                                                   base::StringPiece s2) {
  return base::UTF8ToUTF16(FormatErrorMessageInternal(format, {s1, s2}));
}

std::u16string ErrorUtils::FormatErrorMessageUTF16(base::StringPiece format,
                                                   base::StringPiece s1,
                                                   base::StringPiece s2,
                                                   base::StringPiece s3) {
  return base::UTF8ToUTF16(FormatErrorMessageInternal(format, {s1, s2, s3}));
}

}  // namespace extensions