LCOV - code coverage report
Current view: top level - toolkit/components/protobuf/src/google/protobuf/stubs - substitute.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 49 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 3 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13