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

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2023 Project CHIP Authors
       4              :  *    All rights reserved.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : #pragma once
      19              : 
      20              : #include <lib/support/Span.h>
      21              : 
      22              : namespace chip {
      23              : 
      24              : /// Provides the ability to split a given string by a character.
      25              : ///
      26              : /// Converts things like:
      27              : ///   "a,b,c" split by ',': "a", "b", "c"
      28              : ///   ",b,c" split by ',': "", "b", "c"
      29              : ///   "a,,c" split by ',': "a", "", "c"
      30              : ///   "a," split by ',': "a", ""
      31              : ///   ",a" split by ',': "", "a"
      32              : ///
      33              : ///
      34              : /// WARNING: WILL DESTRUCTIVELY MODIFY THE STRING IN PLACE
      35              : ///
      36              : class StringSplitter
      37              : {
      38              : public:
      39            9 :     StringSplitter(const char * s, char separator) : mNext(s), mSeparator(separator)
      40              :     {
      41            9 :         if ((mNext != nullptr) && (*mNext == '\0'))
      42              :         {
      43            1 :             mNext = nullptr; // end of string right away
      44              :         }
      45            9 :     }
      46              : 
      47              :     /// Returns the next character span
      48              :     ///
      49              :     /// out - contains the next element or a nullptr/0 sized span if
      50              :     ///       no elements available
      51              :     ///
      52              :     /// Returns true if an element is available, false otherwise.
      53           31 :     bool Next(CharSpan & out)
      54              :     {
      55           31 :         if (mNext == nullptr)
      56              :         {
      57           12 :             out = CharSpan();
      58           12 :             return false; // nothing left
      59              :         }
      60              : 
      61           19 :         const char * end = mNext;
      62           49 :         while ((*end != '\0') && (*end != mSeparator))
      63              :         {
      64           30 :             end++;
      65              :         }
      66              : 
      67           19 :         if (*end != '\0')
      68              :         {
      69              :             // intermediate element
      70           12 :             out   = CharSpan(mNext, static_cast<size_t>(end - mNext));
      71           12 :             mNext = end + 1;
      72              :         }
      73              :         else
      74              :         {
      75              :             // last element
      76            7 :             out   = CharSpan::fromCharString(mNext);
      77            7 :             mNext = nullptr;
      78              :         }
      79              : 
      80           19 :         return true;
      81              :     }
      82              : 
      83              : protected:
      84              :     const char * mNext; // start of next element to return by Next()
      85              :     const char mSeparator;
      86              : };
      87              : 
      88              : } // namespace chip
        

Generated by: LCOV version 2.0-1