2018-05-01 21:50:34 +02:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-01 01:56:10 +01:00
|
|
|
#include "characters.h"
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
|
|
|
#include <optional>
|
2018-03-01 01:56:10 +01:00
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-03-01 01:56:10 +01:00
|
|
|
|
|
|
|
std::optional<int> UTF8CharacterBytes(const char *p) {
|
|
|
|
if ((*p & 0x80) == 0) {
|
2018-03-14 00:47:48 +01:00
|
|
|
return {1};
|
2018-03-01 01:56:10 +01:00
|
|
|
}
|
|
|
|
if ((*p & 0xf8) == 0xf0) {
|
|
|
|
if ((p[1] & 0xc0) == 0x80 && (p[2] & 0xc0) == 0x80 &&
|
|
|
|
(p[3] & 0xc0) == 0x80) {
|
|
|
|
return {4};
|
|
|
|
}
|
|
|
|
} else if ((*p & 0xf0) == 0xe0) {
|
|
|
|
if ((p[1] & 0xc0) == 0x80 && (p[2] & 0xc0) == 0x80) {
|
|
|
|
return {3};
|
|
|
|
}
|
|
|
|
} else if ((*p & 0xe0) == 0xc0) {
|
|
|
|
if ((p[1] & 0xc0) == 0x80) {
|
|
|
|
return {2};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<int> EUC_JPCharacterBytes(const char *p) {
|
|
|
|
int b1 = *p & 0xff;
|
|
|
|
if (b1 <= 0x7f) {
|
|
|
|
return {1};
|
|
|
|
}
|
|
|
|
if (b1 >= 0xa1 && b1 <= 0xfe) {
|
|
|
|
int b2 = p[1] & 0xff;
|
|
|
|
if (b2 >= 0xa1 && b2 <= 0xfe) {
|
|
|
|
// JIS X 0208 (code set 1)
|
|
|
|
return {2};
|
|
|
|
}
|
|
|
|
} else if (b1 == 0x8e) {
|
|
|
|
int b2 = p[1] & 0xff;
|
|
|
|
if (b2 >= 0xa1 && b2 <= 0xdf) {
|
|
|
|
// upper half JIS 0201 (half-width kana, code set 2)
|
|
|
|
return {2};
|
|
|
|
}
|
|
|
|
} else if (b1 == 0x8f) {
|
|
|
|
int b2 = p[1] & 0xff;
|
|
|
|
int b3 = p[2] & 0xff;
|
|
|
|
if (b2 >= 0xa1 && b2 <= 0xfe && b3 >= 0xa1 && b3 <= 0xfe) {
|
|
|
|
// JIS X 0212 (code set 3)
|
|
|
|
return {3};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
std::optional<std::size_t> CountCharacters(
|
|
|
|
const char *p, std::size_t bytes, std::optional<int> (*cbf)(const char *)) {
|
|
|
|
std::size_t chars{0};
|
2018-03-01 01:56:10 +01:00
|
|
|
const char *limit{p + bytes};
|
|
|
|
while (p < limit) {
|
|
|
|
++chars;
|
2018-03-14 00:59:30 +01:00
|
|
|
std::optional<int> cb{cbf(p)};
|
|
|
|
if (!cb.has_value()) {
|
2018-03-01 01:56:10 +01:00
|
|
|
return {};
|
|
|
|
}
|
2018-03-14 00:59:30 +01:00
|
|
|
p += *cb;
|
2018-03-01 01:56:10 +01:00
|
|
|
}
|
|
|
|
return {chars};
|
|
|
|
}
|
2018-05-02 22:48:12 +02:00
|
|
|
|
2018-07-20 00:35:55 +02:00
|
|
|
std::string QuoteCharacterLiteral(
|
2018-07-20 00:42:00 +02:00
|
|
|
const std::string &str, bool doubleDoubleQuotes, bool doubleBackslash) {
|
2018-07-07 00:12:33 +02:00
|
|
|
std::string result{'"'};
|
2018-07-11 02:09:07 +02:00
|
|
|
const auto emit{[&](char ch) { result += ch; }};
|
2018-07-07 00:12:33 +02:00
|
|
|
for (char ch : str) {
|
2018-07-20 00:42:00 +02:00
|
|
|
EmitQuotedChar(ch, emit, emit, doubleDoubleQuotes, doubleBackslash);
|
2018-07-07 00:12:33 +02:00
|
|
|
}
|
|
|
|
result += '"';
|
|
|
|
return result;
|
|
|
|
}
|
2018-05-02 22:48:12 +02:00
|
|
|
} // namespace Fortran::parser
|