substitute.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. #include <google/protobuf/stubs/substitute.h>
  32. #include <google/protobuf/stubs/strutil.h>
  33. #include <google/protobuf/stubs/stl_util.h>
  34. namespace google {
  35. namespace protobuf {
  36. namespace strings {
  37. using internal::SubstituteArg;
  38. // Returns the number of args in arg_array which were passed explicitly
  39. // to Substitute().
  40. static int CountSubstituteArgs(const SubstituteArg* const* args_array) {
  41. int count = 0;
  42. while (args_array[count] != NULL && args_array[count]->size() != -1) {
  43. ++count;
  44. }
  45. return count;
  46. }
  47. string Substitute(
  48. const char* format,
  49. const SubstituteArg& arg0, const SubstituteArg& arg1,
  50. const SubstituteArg& arg2, const SubstituteArg& arg3,
  51. const SubstituteArg& arg4, const SubstituteArg& arg5,
  52. const SubstituteArg& arg6, const SubstituteArg& arg7,
  53. const SubstituteArg& arg8, const SubstituteArg& arg9) {
  54. string result;
  55. SubstituteAndAppend(&result, format, arg0, arg1, arg2, arg3, arg4,
  56. arg5, arg6, arg7, arg8, arg9);
  57. return result;
  58. }
  59. void SubstituteAndAppend(
  60. string* output, const char* format,
  61. const SubstituteArg& arg0, const SubstituteArg& arg1,
  62. const SubstituteArg& arg2, const SubstituteArg& arg3,
  63. const SubstituteArg& arg4, const SubstituteArg& arg5,
  64. const SubstituteArg& arg6, const SubstituteArg& arg7,
  65. const SubstituteArg& arg8, const SubstituteArg& arg9) {
  66. const SubstituteArg* const args_array[] = {
  67. &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8, &arg9, NULL
  68. };
  69. // Determine total size needed.
  70. int size = 0;
  71. for (int i = 0; format[i] != '\0'; i++) {
  72. if (format[i] == '$') {
  73. if (ascii_isdigit(format[i+1])) {
  74. int index = format[i+1] - '0';
  75. if (args_array[index]->size() == -1) {
  76. GOOGLE_LOG(DFATAL)
  77. << "strings::Substitute format string invalid: asked for \"$"
  78. << index << "\", but only " << CountSubstituteArgs(args_array)
  79. << " args were given. Full format string was: \""
  80. << CEscape(format) << "\".";
  81. return;
  82. }
  83. size += args_array[index]->size();
  84. ++i; // Skip next char.
  85. } else if (format[i+1] == '$') {
  86. ++size;
  87. ++i; // Skip next char.
  88. } else {
  89. GOOGLE_LOG(DFATAL)
  90. << "Invalid strings::Substitute() format string: \""
  91. << CEscape(format) << "\".";
  92. return;
  93. }
  94. } else {
  95. ++size;
  96. }
  97. }
  98. if (size == 0) return;
  99. // Build the string.
  100. int original_size = output->size();
  101. STLStringResizeUninitialized(output, original_size + size);
  102. char* target = string_as_array(output) + original_size;
  103. for (int i = 0; format[i] != '\0'; i++) {
  104. if (format[i] == '$') {
  105. if (ascii_isdigit(format[i+1])) {
  106. unsigned int index = format[i+1] - '0';
  107. assert(index < 10);
  108. const SubstituteArg* src = args_array[index];
  109. memcpy(target, src->data(), src->size());
  110. target += src->size();
  111. ++i; // Skip next char.
  112. } else if (format[i+1] == '$') {
  113. *target++ = '$';
  114. ++i; // Skip next char.
  115. }
  116. } else {
  117. *target++ = format[i];
  118. }
  119. }
  120. GOOGLE_DCHECK_EQ(target - output->data(), output->size());
  121. }
  122. } // namespace strings
  123. } // namespace protobuf
  124. } // namespace google