henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | */ |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/string_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 13 | #include "absl/strings/string_view.h" |
| 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 15 | namespace rtc { |
| 16 | |
Ali Tofigh | e5b2220 | 2022-03-24 23:08:32 | [diff] [blame] | 17 | size_t strcpyn(char* buffer, size_t buflen, absl::string_view source) { |
Niels Möller | d189252 | 2018-10-17 11:39:01 | [diff] [blame] | 18 | if (buflen <= 0) |
| 19 | return 0; |
| 20 | |
Ali Tofigh | e5b2220 | 2022-03-24 23:08:32 | [diff] [blame] | 21 | size_t srclen = source.length(); |
Niels Möller | d189252 | 2018-10-17 11:39:01 | [diff] [blame] | 22 | if (srclen >= buflen) { |
| 23 | srclen = buflen - 1; |
| 24 | } |
Ali Tofigh | e5b2220 | 2022-03-24 23:08:32 | [diff] [blame] | 25 | memcpy(buffer, source.data(), srclen); |
Niels Möller | d189252 | 2018-10-17 11:39:01 | [diff] [blame] | 26 | buffer[srclen] = 0; |
| 27 | return srclen; |
| 28 | } |
| 29 | |
Jonas Olsson | 7439534 | 2018-04-03 10:22:07 | [diff] [blame] | 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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 37 | } // namespace rtc |