forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringRefUtils.cpp
More file actions
46 lines (36 loc) · 1.27 KB
/
StringRefUtils.cpp
File metadata and controls
46 lines (36 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/StringRefUtils.h"
#include "hermes/Support/UTF8.h"
#include "llvh/ADT/SmallString.h"
#include "llvh/Support/ConvertUTF.h"
#include "llvh/Support/raw_ostream.h"
namespace hermes {
namespace vm {
using llvh::ConversionResult;
using llvh::UTF16;
using llvh::UTF8;
UTF16Ref createUTF16Ref(const char16_t *str) {
return UTF16Ref(str, utf16_traits::length(str));
}
ASCIIRef createASCIIRef(const char *str) {
return ASCIIRef(str, ascii_traits::length(str));
}
llvh::raw_ostream &operator<<(llvh::raw_ostream &OS, ASCIIRef asciiRef) {
return OS << llvh::StringRef(asciiRef.data(), asciiRef.size());
}
/// Print the given UTF-16. We just assume UTF-8 output to avoid the increased
/// binary size of supporting multiple encodings.
llvh::raw_ostream &operator<<(llvh::raw_ostream &OS, UTF16Ref u16ref) {
// Note this assumes that the desired output encoding is UTF-8, which may
// not be a valid assumption if outputting to a tty.
std::string narrowStr;
convertUTF16ToUTF8WithReplacements(narrowStr, u16ref);
return OS << narrowStr;
}
} // namespace vm
} // namespace hermes