诸暨麻将添加redis
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

139 líneas
5.1 KiB

  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/logging.h>
  33. #include <google/protobuf/stubs/strutil.h>
  34. #include <google/protobuf/stubs/stl_util.h>
  35. namespace google {
  36. namespace protobuf {
  37. namespace strings {
  38. using internal::SubstituteArg;
  39. // Returns the number of args in arg_array which were passed explicitly
  40. // to Substitute().
  41. static int CountSubstituteArgs(const SubstituteArg* const* args_array) {
  42. int count = 0;
  43. while (args_array[count] != nullptr && args_array[count]->size() != -1) {
  44. ++count;
  45. }
  46. return count;
  47. }
  48. string Substitute(
  49. const char* format,
  50. const SubstituteArg& arg0, const SubstituteArg& arg1,
  51. const SubstituteArg& arg2, const SubstituteArg& arg3,
  52. const SubstituteArg& arg4, const SubstituteArg& arg5,
  53. const SubstituteArg& arg6, const SubstituteArg& arg7,
  54. const SubstituteArg& arg8, const SubstituteArg& arg9) {
  55. string result;
  56. SubstituteAndAppend(&result, format, arg0, arg1, arg2, arg3, arg4,
  57. arg5, arg6, arg7, arg8, arg9);
  58. return result;
  59. }
  60. void SubstituteAndAppend(
  61. string* output, const char* format,
  62. const SubstituteArg& arg0, const SubstituteArg& arg1,
  63. const SubstituteArg& arg2, const SubstituteArg& arg3,
  64. const SubstituteArg& arg4, const SubstituteArg& arg5,
  65. const SubstituteArg& arg6, const SubstituteArg& arg7,
  66. const SubstituteArg& arg8, const SubstituteArg& arg9) {
  67. const SubstituteArg* const args_array[] = {
  68. &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8, &arg9, nullptr
  69. };
  70. // Determine total size needed.
  71. int size = 0;
  72. for (int i = 0; format[i] != '\0'; i++) {
  73. if (format[i] == '$') {
  74. if (ascii_isdigit(format[i+1])) {
  75. int index = format[i+1] - '0';
  76. if (args_array[index]->size() == -1) {
  77. GOOGLE_LOG(DFATAL)
  78. << "strings::Substitute format string invalid: asked for \"$"
  79. << index << "\", but only " << CountSubstituteArgs(args_array)
  80. << " args were given. Full format string was: \""
  81. << CEscape(format) << "\".";
  82. return;
  83. }
  84. size += args_array[index]->size();
  85. ++i; // Skip next char.
  86. } else if (format[i+1] == '$') {
  87. ++size;
  88. ++i; // Skip next char.
  89. } else {
  90. GOOGLE_LOG(DFATAL)
  91. << "Invalid strings::Substitute() format string: \""
  92. << CEscape(format) << "\".";
  93. return;
  94. }
  95. } else {
  96. ++size;
  97. }
  98. }
  99. if (size == 0) return;
  100. // Build the string.
  101. int original_size = output->size();
  102. STLStringResizeUninitialized(output, original_size + size);
  103. char* target = string_as_array(output) + original_size;
  104. for (int i = 0; format[i] != '\0'; i++) {
  105. if (format[i] == '$') {
  106. if (ascii_isdigit(format[i+1])) {
  107. unsigned int index = format[i+1] - '0';
  108. assert(index < 10);
  109. const SubstituteArg* src = args_array[index];
  110. memcpy(target, src->data(), src->size());
  111. target += src->size();
  112. ++i; // Skip next char.
  113. } else if (format[i+1] == '$') {
  114. *target++ = '$';
  115. ++i; // Skip next char.
  116. }
  117. } else {
  118. *target++ = format[i];
  119. }
  120. }
  121. GOOGLE_DCHECK_EQ(target - output->data(), output->size());
  122. }
  123. } // namespace strings
  124. } // namespace protobuf
  125. } // namespace google