xref: /aosp_15_r20/external/webrtc/rtc_base/string_utils.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/string_utils.h"
12 
13 #include "absl/strings/string_view.h"
14 
15 namespace rtc {
16 
strcpyn(char * buffer,size_t buflen,absl::string_view source)17 size_t strcpyn(char* buffer, size_t buflen, absl::string_view source) {
18   if (buflen <= 0)
19     return 0;
20 
21   size_t srclen = source.length();
22   if (srclen >= buflen) {
23     srclen = buflen - 1;
24   }
25   memcpy(buffer, source.data(), srclen);
26   buffer[srclen] = 0;
27   return srclen;
28 }
29 
ToHex(const int i)30 std::string ToHex(const int i) {
31   char buffer[50];
32   snprintf(buffer, sizeof(buffer), "%x", i);
33 
34   return std::string(buffer);
35 }
36 
37 }  // namespace rtc
38