hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 11 | #import "NSString+RTCStdString.h" |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 12 | |
Ali Tofigh | 6364d08 | 2022-03-14 13:32:04 +0100 | [diff] [blame] | 13 | #include "absl/strings/string_view.h" |
| 14 | |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 15 | @implementation NSString (RTCStdString) |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 16 | |
| 17 | - (std::string)stdString { |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 18 | return [NSString rtc_stdStringForString:self]; |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 21 | + (std::string)rtc_stdStringForString:(NSString *)nsString { |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 22 | NSData *charData = [nsString dataUsingEncoding:NSUTF8StringEncoding]; |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 23 | return std::string(reinterpret_cast<const char *>(charData.bytes), charData.length); |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 26 | + (NSString *)rtc_stringForStdString:(const std::string &)stdString { |
Jon Hjelle | 29d5e57 | 2016-01-06 11:49:11 -0800 | [diff] [blame] | 27 | // std::string may contain null termination character so we construct |
| 28 | // using length. |
| 29 | return [[NSString alloc] initWithBytes:stdString.data() |
| 30 | length:stdString.length() |
| 31 | encoding:NSUTF8StringEncoding]; |
| 32 | } |
| 33 | |
hjon | aa32c3e | 2015-12-13 19:58:11 -0800 | [diff] [blame] | 34 | @end |
Ali Tofigh | 6364d08 | 2022-03-14 13:32:04 +0100 | [diff] [blame] | 35 | |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 36 | @implementation NSString (RTCAbslStringView) |
Ali Tofigh | 6364d08 | 2022-03-14 13:32:04 +0100 | [diff] [blame] | 37 | |
Byoungchan Lee | 181ea6e | 2022-05-13 19:02:07 +0900 | [diff] [blame^] | 38 | + (NSString *)rtc_stringForAbslStringView:(const absl::string_view)abslStringView { |
Ali Tofigh | 6364d08 | 2022-03-14 13:32:04 +0100 | [diff] [blame] | 39 | return [[NSString alloc] initWithBytes:abslStringView.data() |
| 40 | length:abslStringView.length() |
| 41 | encoding:NSUTF8StringEncoding]; |
| 42 | } |
| 43 | |
| 44 | @end |