Matter SDK Coverage Report
Current view: top level - lib/support - SafeString.h (source / functions) Coverage Total Hit
Test: SHA:4d2388ac7eed75b2fe5e05e20de377999c632502 Lines: 100.0 % 8 8
Test Date: 2025-07-27 07:17:09 Functions: 100.0 % 11 11

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2021 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : /**
      19              :  *    @file
      20              :  *      Utilities for safely working with strings.
      21              :  *
      22              :  */
      23              : 
      24              : #pragma once
      25              : 
      26              : #include <algorithm>
      27              : #include <utility>
      28              : 
      29              : #include <lib/support/CodeUtils.h>
      30              : 
      31              : namespace chip {
      32              : 
      33              : /**
      34              :  * A function that determines, at compile time, the maximal length (not
      35              :  * counting the null terminator) of a list of literal C strings.  Suitable for
      36              :  * determining sizes of buffers that might need to hold any of the given
      37              :  * strings.
      38              :  *
      39              :  * Do NOT pass things that are not string literals to this function.
      40              :  *
      41              :  * Use like:
      42              :  *   constexpr size_t maxLen = MaxStringLength("abc", "defhij", "something");
      43              :  */
      44            3 : constexpr size_t MaxStringLength()
      45              : {
      46            3 :     return 0;
      47              : }
      48              : 
      49              : template <size_t FirstLength, typename... RestOfTypes>
      50            5 : constexpr size_t MaxStringLength(const char (&)[FirstLength], RestOfTypes &&... aArgs)
      51              : {
      52              :     // Subtract 1 because we are not counting the null-terminator.
      53            5 :     return std::max(FirstLength - 1, MaxStringLength(std::forward<RestOfTypes>(aArgs)...));
      54              : }
      55              : 
      56              : /**
      57              :  * A function that determines, at compile time, the total length (not
      58              :  * counting the null terminator) of a list of literal C strings. Suitable for
      59              :  * determining sizes of buffers that might need to hold all of the given
      60              :  * strings.
      61              :  *
      62              :  * Do NOT pass things that are not string literals to this function.
      63              :  *
      64              :  * Use like:
      65              :  *   constexpr size_t totalLen = TotalStringLength("abc", "defhij", "something");
      66              :  */
      67            3 : constexpr size_t TotalStringLength()
      68              : {
      69            3 :     return 0;
      70              : }
      71              : 
      72              : template <size_t FirstLength, typename... RestOfTypes>
      73            5 : constexpr size_t TotalStringLength(const char (&)[FirstLength], RestOfTypes &&... aArgs)
      74              : {
      75              :     // Subtract 1 because we are not counting the null-terminator.
      76            5 :     return FirstLength - 1 + TotalStringLength(std::forward<RestOfTypes>(aArgs)...);
      77              : }
      78              : 
      79              : } // namespace chip
        

Generated by: LCOV version 2.0-1