browser/components/translation/cld2/internal/fixunicodevalue.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/translation/cld2/internal/fixunicodevalue.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,54 @@
     1.4 +// Copyright 2013 Google Inc. All Rights Reserved.
     1.5 +//
     1.6 +// Licensed under the Apache License, Version 2.0 (the "License");
     1.7 +// you may not use this file except in compliance with the License.
     1.8 +// You may obtain a copy of the License at
     1.9 +//
    1.10 +//     http://www.apache.org/licenses/LICENSE-2.0
    1.11 +//
    1.12 +// Unless required by applicable law or agreed to in writing, software
    1.13 +// distributed under the License is distributed on an "AS IS" BASIS,
    1.14 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.15 +// See the License for the specific language governing permissions and
    1.16 +// limitations under the License.
    1.17 +
    1.18 +//
    1.19 +// Routine that maps a Unicode code point to an interchange-valid one
    1.20 +//
    1.21 +
    1.22 +#include "fixunicodevalue.h"
    1.23 +#include "integral_types.h"
    1.24 +
    1.25 +namespace CLD2 {
    1.26 +
    1.27 +// Guarantees that the resulting output value is interchange valid
    1.28 +//  00-FF; map to spaces or MS CP1252
    1.29 +//  D800-DFFF; surrogates
    1.30 +//  FDD0-FDEF; non-characters
    1.31 +//  xxFFFE-xxFFFF; non-characters
    1.32 +char32 FixUnicodeValue(char32 uv) {
    1.33 +  uint32 uuv = static_cast<uint32>(uv);
    1.34 +  if (uuv < 0x0100) {
    1.35 +    return kMapFullMicrosoft1252OrSpace[uuv];
    1.36 +  }
    1.37 +  if (uuv < 0xD800) {
    1.38 +    return uv;
    1.39 +  }
    1.40 +  if ((uuv & ~0x0F) == 0xFDD0) {              // non-characters
    1.41 +    return 0xFFFD;
    1.42 +  }
    1.43 +  if ((uuv & ~0x0F) == 0xFDE0) {              // non-characters
    1.44 +    return 0xFFFD;
    1.45 +  }
    1.46 +  if ((uuv & 0x00FFFE) == 0xFFFE) {           // non-characters
    1.47 +    return 0xFFFD;
    1.48 +  }
    1.49 +  if ((0xE000 <= uuv) && (uuv <= 0x10FFFF))  {
    1.50 +    return uv;
    1.51 +  }
    1.52 +  // surrogates and negative and > 0x10FFFF all land here
    1.53 +  return 0xFFFD;
    1.54 +}
    1.55 +
    1.56 +}       // End namespace CLD2
    1.57 +

mercurial