11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_BASE_STRINGS_H_
61cb0ef41Sopenharmony_ci#define V8_BASE_STRINGS_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/base/base-export.h"
91cb0ef41Sopenharmony_ci#include "src/base/macros.h"
101cb0ef41Sopenharmony_ci#include "src/base/vector.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cinamespace v8 {
131cb0ef41Sopenharmony_cinamespace base {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Latin1/UTF-16 constants
161cb0ef41Sopenharmony_ci// Code-point values in Unicode 4.0 are 21 bits wide.
171cb0ef41Sopenharmony_ci// Code units in UTF-16 are 16 bits wide.
181cb0ef41Sopenharmony_ciusing uc16 = uint16_t;
191cb0ef41Sopenharmony_ciusing uc32 = uint32_t;
201cb0ef41Sopenharmony_ciconstexpr int kUC16Size = sizeof(uc16);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciV8_BASE_EXPORT int PRINTF_FORMAT(2, 0)
231cb0ef41Sopenharmony_ci    VSNPrintF(Vector<char> str, const char* format, va_list args);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci// Safe formatting print. Ensures that str is always null-terminated.
261cb0ef41Sopenharmony_ci// Returns the number of chars written, or -1 if output was truncated.
271cb0ef41Sopenharmony_ciV8_BASE_EXPORT int PRINTF_FORMAT(2, 3)
281cb0ef41Sopenharmony_ci    SNPrintF(Vector<char> str, const char* format, ...);
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciV8_BASE_EXPORT void StrNCpy(base::Vector<char> dest, const char* src, size_t n);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// Returns the value (0 .. 15) of a hexadecimal character c.
331cb0ef41Sopenharmony_ci// If c is not a legal hexadecimal character, returns a value < 0.
341cb0ef41Sopenharmony_ciinline int HexValue(uc32 c) {
351cb0ef41Sopenharmony_ci  c -= '0';
361cb0ef41Sopenharmony_ci  if (static_cast<unsigned>(c) <= 9) return c;
371cb0ef41Sopenharmony_ci  c = (c | 0x20) - ('a' - '0');  // detect 0x11..0x16 and 0x31..0x36.
381cb0ef41Sopenharmony_ci  if (static_cast<unsigned>(c) <= 5) return c + 10;
391cb0ef41Sopenharmony_ci  return -1;
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciinline char HexCharOfValue(int value) {
431cb0ef41Sopenharmony_ci  DCHECK(0 <= value && value <= 16);
441cb0ef41Sopenharmony_ci  if (value < 10) return value + '0';
451cb0ef41Sopenharmony_ci  return value - 10 + 'A';
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci}  // namespace base
491cb0ef41Sopenharmony_ci}  // namespace v8
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci#endif  // V8_BASE_STRINGS_H_
52