michael@0: // Copyright 2013 Google Inc. All Rights Reserved. michael@0: // michael@0: // Licensed under the Apache License, Version 2.0 (the "License"); michael@0: // you may not use this file except in compliance with the License. michael@0: // You may obtain a copy of the License at michael@0: // michael@0: // http://www.apache.org/licenses/LICENSE-2.0 michael@0: // michael@0: // Unless required by applicable law or agreed to in writing, software michael@0: // distributed under the License is distributed on an "AS IS" BASIS, michael@0: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: // See the License for the specific language governing permissions and michael@0: // limitations under the License. michael@0: michael@0: // michael@0: // Routine that maps a Unicode code point to an interchange-valid one michael@0: // michael@0: michael@0: #include "fixunicodevalue.h" michael@0: #include "integral_types.h" michael@0: michael@0: namespace CLD2 { michael@0: michael@0: // Guarantees that the resulting output value is interchange valid michael@0: // 00-FF; map to spaces or MS CP1252 michael@0: // D800-DFFF; surrogates michael@0: // FDD0-FDEF; non-characters michael@0: // xxFFFE-xxFFFF; non-characters michael@0: char32 FixUnicodeValue(char32 uv) { michael@0: uint32 uuv = static_cast(uv); michael@0: if (uuv < 0x0100) { michael@0: return kMapFullMicrosoft1252OrSpace[uuv]; michael@0: } michael@0: if (uuv < 0xD800) { michael@0: return uv; michael@0: } michael@0: if ((uuv & ~0x0F) == 0xFDD0) { // non-characters michael@0: return 0xFFFD; michael@0: } michael@0: if ((uuv & ~0x0F) == 0xFDE0) { // non-characters michael@0: return 0xFFFD; michael@0: } michael@0: if ((uuv & 0x00FFFE) == 0xFFFE) { // non-characters michael@0: return 0xFFFD; michael@0: } michael@0: if ((0xE000 <= uuv) && (uuv <= 0x10FFFF)) { michael@0: return uv; michael@0: } michael@0: // surrogates and negative and > 0x10FFFF all land here michael@0: return 0xFFFD; michael@0: } michael@0: michael@0: } // End namespace CLD2 michael@0: