blob: df253c709c01a870a1e490d446905268a3ab5df8 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2013 The Chromium Authors
[email protected]51bcc5d2013-04-24 01:41:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]e7bba5f82013-04-10 20:10:524
Tom Sepezf837741b2024-07-02 22:47:515#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/350788890): Remove this and spanify to fix the errors.
7#pragma allow_unsafe_buffers
8#endif
9
[email protected]318076b2013-04-18 21:19:4510#ifndef URL_URL_TEST_UTILS_H_
11#define URL_URL_TEST_UTILS_H_
12
[email protected]e7bba5f82013-04-10 20:10:5213// Convenience functions for string conversions.
14// These are mostly intended for use in unit tests.
15
[email protected]e7bba5f82013-04-10 20:10:5216#include <string>
17
brettw1b8582f2016-11-03 20:37:1718#include "base/strings/utf_string_conversions.h"
[email protected]e7bba5f82013-04-10 20:10:5219#include "testing/gtest/include/gtest/gtest.h"
[email protected]318076b2013-04-18 21:19:4520#include "url/url_canon_internal.h"
[email protected]e7bba5f82013-04-10 20:10:5221
[email protected]0318f922014-04-22 00:09:2322namespace url {
23
24namespace test_utils {
[email protected]e7bba5f82013-04-10 20:10:5225
brettw1b8582f2016-11-03 20:37:1726// Converts a UTF-16 string from native wchar_t format to char16 by
27// truncating the high 32 bits. This is different than the conversion function
28// in base bacause it passes invalid UTF-16 characters which is important for
29// test purposes. As a result, this is not meant to handle true UTF-32 encoded
30// strings.
Jan Wilken Dörrie739ccc212021-03-11 18:13:0531inline std::u16string TruncateWStringToUTF16(const wchar_t* src) {
32 std::u16string str;
[email protected]e7bba5f82013-04-10 20:10:5233 int length = static_cast<int>(wcslen(src));
34 for (int i = 0; i < length; ++i) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:1235 str.push_back(static_cast<char16_t>(src[i]));
[email protected]e7bba5f82013-04-10 20:10:5236 }
37 return str;
38}
39
[email protected]0318f922014-04-22 00:09:2340} // namespace test_utils
41
42} // namespace url
[email protected]e7bba5f82013-04-10 20:10:5243
[email protected]318076b2013-04-18 21:19:4544#endif // URL_URL_TEST_UTILS_H_