Skip to content

Commit

Permalink
Added a CopyCharSpanToMutableCharSpanTruncate function. (#35360)
Browse files Browse the repository at this point in the history
* Added a CopyCharSpanToMutableCharSpanTruncate function.

* Applied minor renaming suggestion.

* Apply suggestions from code review

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/lib/support/Span.h

Co-authored-by: Arkadiusz Bokowy <[email protected]>

---------

Co-authored-by: Boris Zbarsky <[email protected]>
Co-authored-by: Arkadiusz Bokowy <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent 6144736 commit 19e7aa3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lib/support/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,18 @@ inline CHIP_ERROR CopyCharSpanToMutableCharSpan(CharSpan cspan_to_copy, MutableC
return CHIP_NO_ERROR;
}

/**
* Copies a CharSpan into a MutableCharSpan.
* If the span_to_copy does not fit in out_span, span_to_copy is truncated to fit in out_span.
* @param span_to_copy The CharSpan to copy.
* @param out_span The MutableCharSpan in which span_to_copy is to be copied.
*/
inline void CopyCharSpanToMutableCharSpanWithTruncation(CharSpan span_to_copy, MutableCharSpan & out_span)
{
size_t size_to_copy = std::min(span_to_copy.size(), out_span.size());

memcpy(out_span.data(), span_to_copy.data(), size_to_copy);
out_span.reduce_size(size_to_copy);
}

} // namespace chip

0 comments on commit 19e7aa3

Please sign in to comment.