fe3eb3ed5c
* ICU-13530 copy C/C++ files UTrie2 -> UTrie3 X-SVN-Rev: 40754 * ICU-13530 UTrie3 new files copied from UTrie2: rename types/functions/macros X-SVN-Rev: 40755 * ICU-13530 debug-print building each UTrie2 X-SVN-Rev: 40756 * ICU-13530 remove two-byte-UTF-8 errorValue block; move highValue from end of data array into header; add errorValue to header X-SVN-Rev: 40762 * ICU-13530 UTrie3 U16_NEXT/PREV: errorValue for unpaired surrogates X-SVN-Rev: 40763 * ICU-13530 no more separate values for lead surrogate code units X-SVN-Rev: 40764 * ICU-13530 change from 11:5 trie bits to 10:6 for simpler UTF-8 code X-SVN-Rev: 40766 * ICU-13530 UTrie2 build UTrie3 as well, print sizes X-SVN-Rev: 40767 * ICU-13530 debug-print countSame, sumOverlaps, countInitial X-SVN-Rev: 40768 * ICU-13530 debug-print whether trie is for CanonIterData X-SVN-Rev: 40769 * ICU-13530 no index-shift for BMP data, no separate index-2 for 2-byte UTF-8; builder changes incomplete X-SVN-Rev: 40777 * ICU-13530 remove errorValue and highStart from UNewTrie3 X-SVN-Rev: 40778 * ICU-13530 rewrite UTrie3 builder code X-SVN-Rev: 40783 * ICU-13530 UTrie3 bug fixes X-SVN-Rev: 40788 * ICU-13530 fully re-inline _UTRIE3_U8_NEXT() X-SVN-Rev: 40790 * ICU-13530 find most common all-same data block for dataNullBlock and initialValue X-SVN-Rev: 40792 * ICU-13530 UTrie3 iterator functions take start and return the end of a range, rather than callback call for each range X-SVN-Rev: 40800 * ICU-13530 mask off unused data value bits before building a UTrie3 with values less than 32 bits wide X-SVN-Rev: 40803 * ICU-13530 split utrie3builder.h out of utrie3.h X-SVN-Rev: 40804 * ICU-13530 separate types UTrie3 vs. UTrie3Builder, implement builder as wrapper over C++ class Trie3Builder in .cpp X-SVN-Rev: 40809 * ICU-13530 function to make a UTrie3Builder from a UTrie3 X-SVN-Rev: 40810 * ICU-13530 debug-print some data; some cleanup X-SVN-Rev: 40865 * ICU-13530 BMP 10:6 but supplementary 10:6:4 X-SVN-Rev: 40984 * ICU-13530 move errorValue & highValue to the end of the data table, minimal padding to 4 bytes X-SVN-Rev: 41011 * ICU-13530 index-1 table gap of index-2 null blocks X-SVN-Rev: 41018 * ICU-13530 test with more than 128k compacted data X-SVN-Rev: 41034 * ICU-13530 supplementary bits 11:5:4 saves a little space X-SVN-Rev: 41039 * ICU-13530 supplementary bits 6:5:5:4 instead of gap: about same size but simpler X-SVN-Rev: 41050 * ICU-13530 remove unnecessary utrie3_clone(built trie) X-SVN-Rev: 41058 * ICU-13530 remove unnecessary UTrie3StringIterator X-SVN-Rev: 41059 * ICU-13530 back to UTRIE3_GET...() macros *returning* data values X-SVN-Rev: 41060 * ICU-13530 fast vs. small X-SVN-Rev: 41066 * ICU-13530 always load NFC data, add simple normalization performance test X-SVN-Rev: 41110 * ICU-13530 change normalization main trie to UTrie3 with special values for lead surrogates; forbid non-inert surrogate code *points* because unable to store values different from code *units*; runtime code work around that for code point lookup and iteration; adjust UTS 46 for normalization no longer mapping unpaired surrogates to U+FFFD X-SVN-Rev: 41122 * ICU-13530 simplenormperf bug fix and NFC base line X-SVN-Rev: 41126 * ICU-13530 move normalization getRange skipping lead surrogates to API getRangeSkipLead() X-SVN-Rev: 41182 * ICU-13530 switch CanonIterData and gennorm2 Norms to UTrie3 X-SVN-Rev: 41183 * ICU-13530 remove unused overwrite parameter from setRange() X-SVN-Rev: 41184 * ICU-13530 getRange skip lead -> fixed surrogates X-SVN-Rev: 41219 * ICU-13530 minor cleanup X-SVN-Rev: 41221 * ICU-13530 UTS 46 code map unpaired surrogates to U+FFFD before normalization X-SVN-Rev: 41224 * ICU-13530 minor internal-docs cleanup X-SVN-Rev: 41225 * ICU-13530 rename UTrie3 to UCPTrie, and other name changes X-SVN-Rev: 41226 * ICU-13530 add 8-bit data option; add type-any & valueBits-any for fromBinary(); macros consistently source type then data width X-SVN-Rev: 41234 * ICU-13530 scrub the API docs for the proposal X-SVN-Rev: 41319 * ICU-13530 tag internal definitions as such, or move them to an internal header X-SVN-Rev: 41320 * ICU-13530 Java API skeleton X-SVN-Rev: 41326 * ICU-13530 API feedback: ValueWidth, MutableCodePointTrie, base CodePointMap, ... X-SVN-Rev: 41382 * ICU-13530 add UCPTrie valueWidth field and padding, and combine data pointers into a union X-SVN-Rev: 41408 * ICU-13530 switch some macros to using dataAccess parameter: separate index vs. data lookups, no macro variant for each value width X-SVN-Rev: 41409 * ICU-13530 StringIterator is no longer a java.util.Iterator (bad fit) X-SVN-Rev: 41455 * ICU-13530 CodePointTrie.java code complete X-SVN-Rev: 41518 * ICU-13530 finish Java port incl test; keep C++ parallel * ICU-13530 adjust API for feedback: rename HandleValue to FilterValue, change getRange+getRangeFixedSurr(bool allSurr) to enum RangeOption+getRange(enum option); change remaining C macros to use dataAccess for 16/32/8-bit value widths; fix/clarify some API docs * ICU-13530 add javadoc * ICU-13530 document UCPTrie binary data format * ICU-13530 update .nrm formatVersion 3->4, document change in surrogate handling with new trie * ICU-13530 re-hardcode NFC data * move trie swapper code into new file; add new files to Windows project files; turn off trie debugging * ICU-13530 minor cleanup * ICU-13530 test more range starts; fix a C test leak * ICU-13530 regenerate Java data from scratch * ICU-13530 review feedback changes: API docs typos, more @internal, C++11 field initializers, fix potential leak in MutableCodePointTrie::fromUCPTrie() * ICU-13530 rename interface FilterValue to ValueFilter
338 lines
12 KiB
C++
338 lines
12 KiB
C++
// © 2016 and later: Unicode, Inc. and others.
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
/*
|
|
*******************************************************************************
|
|
*
|
|
* Copyright (C) 2009-2014, International Business Machines
|
|
* Corporation and others. All Rights Reserved.
|
|
*
|
|
*******************************************************************************
|
|
* file name: gennorm2.cpp
|
|
* encoding: UTF-8
|
|
* tab size: 8 (not used)
|
|
* indentation:4
|
|
*
|
|
* created on: 2009nov25
|
|
* created by: Markus W. Scherer
|
|
*
|
|
* This program reads text files that define Unicode normalization,
|
|
* parses them, and builds a binary data file.
|
|
*/
|
|
|
|
#include "unicode/utypes.h"
|
|
#include "n2builder.h"
|
|
|
|
#include <fstream>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string>
|
|
#include <string.h>
|
|
#include "unicode/errorcode.h"
|
|
#include "unicode/localpointer.h"
|
|
#include "unicode/putil.h"
|
|
#include "unicode/uchar.h"
|
|
#include "unicode/unistr.h"
|
|
#include "charstr.h"
|
|
#include "normalizer2impl.h"
|
|
#include "toolutil.h"
|
|
#include "uoptions.h"
|
|
#include "uparse.h"
|
|
|
|
#if UCONFIG_NO_NORMALIZATION
|
|
#include "unewdata.h"
|
|
#endif
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
UBool beVerbose=FALSE, haveCopyright=TRUE;
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
void parseFile(std::ifstream &f, Normalizer2DataBuilder &builder);
|
|
#endif
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
enum {
|
|
HELP_H,
|
|
HELP_QUESTION_MARK,
|
|
VERBOSE,
|
|
COPYRIGHT,
|
|
SOURCEDIR,
|
|
OUTPUT_FILENAME,
|
|
UNICODE_VERSION,
|
|
WRITE_C_SOURCE,
|
|
WRITE_COMBINED_DATA,
|
|
OPT_FAST
|
|
};
|
|
|
|
static UOption options[]={
|
|
UOPTION_HELP_H,
|
|
UOPTION_HELP_QUESTION_MARK,
|
|
UOPTION_VERBOSE,
|
|
UOPTION_COPYRIGHT,
|
|
UOPTION_SOURCEDIR,
|
|
UOPTION_DEF("output", 'o', UOPT_REQUIRES_ARG),
|
|
UOPTION_DEF("unicode", 'u', UOPT_REQUIRES_ARG),
|
|
UOPTION_DEF("csource", '\1', UOPT_NO_ARG),
|
|
UOPTION_DEF("combined", '\1', UOPT_NO_ARG),
|
|
UOPTION_DEF("fast", '\1', UOPT_NO_ARG)
|
|
};
|
|
|
|
extern "C" int
|
|
main(int argc, char* argv[]) {
|
|
U_MAIN_INIT_ARGS(argc, argv);
|
|
|
|
/* preset then read command line options */
|
|
options[SOURCEDIR].value="";
|
|
argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[HELP_H]), options);
|
|
|
|
/* error handling, printing usage message */
|
|
if(argc<0) {
|
|
fprintf(stderr,
|
|
"error in command line argument \"%s\"\n",
|
|
argv[-argc]);
|
|
}
|
|
if(!options[OUTPUT_FILENAME].doesOccur) {
|
|
argc=-1;
|
|
}
|
|
if( argc<2 ||
|
|
options[HELP_H].doesOccur || options[HELP_QUESTION_MARK].doesOccur
|
|
) {
|
|
fprintf(stderr,
|
|
"Usage: %s [-options] infiles+ -o outputfilename\n"
|
|
"\n"
|
|
"Reads the infiles with normalization data and\n"
|
|
"creates a binary file, or a C source file (--csource), with the data,\n"
|
|
"or writes a data file with the combined data (--combined).\n"
|
|
"See http://userguide.icu-project.org/transforms/normalization#TOC-Data-File-Syntax\n"
|
|
"\n"
|
|
"Alternate usage: %s [-options] a.txt b.txt minus p.txt q.txt -o outputfilename\n"
|
|
"\n"
|
|
"Computes the difference of (a, b) minus (p, q) and writes the diff data\n"
|
|
"in input-file syntax to the outputfilename.\n"
|
|
"It is then possible to build (p, q, diff) to get the same data as (a, b).\n"
|
|
"(Useful for computing minimal incremental mapping data files.)\n"
|
|
"\n",
|
|
argv[0], argv[0]);
|
|
fprintf(stderr,
|
|
"Options:\n"
|
|
"\t-h or -? or --help this usage text\n"
|
|
"\t-v or --verbose verbose output\n"
|
|
"\t-c or --copyright include a copyright notice\n"
|
|
"\t-u or --unicode Unicode version, followed by the version like 5.2.0\n");
|
|
fprintf(stderr,
|
|
"\t-s or --sourcedir source directory, followed by the path\n"
|
|
"\t-o or --output output filename\n"
|
|
"\t --csource writes a C source file with initializers\n"
|
|
"\t --combined writes a .txt file (input-file syntax) with the\n"
|
|
"\t combined data from all of the input files\n");
|
|
fprintf(stderr,
|
|
"\t --fast optimize the data for fast normalization,\n"
|
|
"\t which might increase its size (Writes fully decomposed\n"
|
|
"\t regular mappings instead of delta mappings.\n"
|
|
"\t You should measure the runtime speed to make sure that\n"
|
|
"\t this is a good trade-off.)\n");
|
|
return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
|
|
}
|
|
|
|
beVerbose=options[VERBOSE].doesOccur;
|
|
haveCopyright=options[COPYRIGHT].doesOccur;
|
|
|
|
IcuToolErrorCode errorCode("gennorm2/main()");
|
|
|
|
#if UCONFIG_NO_NORMALIZATION
|
|
|
|
fprintf(stderr,
|
|
"gennorm2 writes a dummy binary data file "
|
|
"because UCONFIG_NO_NORMALIZATION is set, \n"
|
|
"see icu/source/common/unicode/uconfig.h\n");
|
|
udata_createDummy(NULL, NULL, options[OUTPUT_FILENAME].value, errorCode);
|
|
// Should not return an error since this is the expected behaviour if UCONFIG_NO_NORMALIZATION is on.
|
|
// return U_UNSUPPORTED_ERROR;
|
|
return 0;
|
|
|
|
#else
|
|
|
|
LocalPointer<Normalizer2DataBuilder> b1(new Normalizer2DataBuilder(errorCode), errorCode);
|
|
LocalPointer<Normalizer2DataBuilder> b2;
|
|
LocalPointer<Normalizer2DataBuilder> diff;
|
|
Normalizer2DataBuilder *builder = b1.getAlias();
|
|
errorCode.assertSuccess();
|
|
|
|
if(options[UNICODE_VERSION].doesOccur) {
|
|
builder->setUnicodeVersion(options[UNICODE_VERSION].value);
|
|
}
|
|
|
|
if(options[OPT_FAST].doesOccur) {
|
|
builder->setOptimization(Normalizer2DataBuilder::OPTIMIZE_FAST);
|
|
}
|
|
|
|
// prepare the filename beginning with the source dir
|
|
CharString filename(options[SOURCEDIR].value, errorCode);
|
|
int32_t pathLength=filename.length();
|
|
if( pathLength>0 &&
|
|
filename[pathLength-1]!=U_FILE_SEP_CHAR &&
|
|
filename[pathLength-1]!=U_FILE_ALT_SEP_CHAR
|
|
) {
|
|
filename.append(U_FILE_SEP_CHAR, errorCode);
|
|
pathLength=filename.length();
|
|
}
|
|
|
|
bool doMinus = false;
|
|
for(int i=1; i<argc; ++i) {
|
|
printf("gennorm2: processing %s\n", argv[i]);
|
|
if(strcmp(argv[i], "minus") == 0) {
|
|
if(doMinus) {
|
|
fprintf(stderr, "gennorm2 error: only one 'minus' can be specified\n");
|
|
exit(U_ILLEGAL_ARGUMENT_ERROR);
|
|
}
|
|
// Data from previous input files has been collected in b1.
|
|
// Collect data from further input files in b2.
|
|
b2.adoptInsteadAndCheckErrorCode(new Normalizer2DataBuilder(errorCode), errorCode);
|
|
diff.adoptInsteadAndCheckErrorCode(new Normalizer2DataBuilder(errorCode), errorCode);
|
|
errorCode.assertSuccess();
|
|
builder = b2.getAlias();
|
|
if(options[UNICODE_VERSION].doesOccur) {
|
|
builder->setUnicodeVersion(options[UNICODE_VERSION].value);
|
|
}
|
|
if(options[OPT_FAST].doesOccur) {
|
|
builder->setOptimization(Normalizer2DataBuilder::OPTIMIZE_FAST);
|
|
}
|
|
doMinus = true;
|
|
continue;
|
|
}
|
|
filename.append(argv[i], errorCode);
|
|
std::ifstream f(filename.data());
|
|
if(f.fail()) {
|
|
fprintf(stderr, "gennorm2 error: unable to open %s\n", filename.data());
|
|
exit(U_FILE_ACCESS_ERROR);
|
|
}
|
|
builder->setOverrideHandling(Normalizer2DataBuilder::OVERRIDE_PREVIOUS);
|
|
parseFile(f, *builder);
|
|
filename.truncate(pathLength);
|
|
}
|
|
|
|
if(doMinus) {
|
|
Normalizer2DataBuilder::computeDiff(*b1, *b2, *diff);
|
|
diff->writeDataFile(options[OUTPUT_FILENAME].value, /* writeRemoved= */ true);
|
|
} else if(options[WRITE_COMBINED_DATA].doesOccur) {
|
|
builder->writeDataFile(options[OUTPUT_FILENAME].value, /* writeRemoved= */ false);
|
|
} else if(options[WRITE_C_SOURCE].doesOccur) {
|
|
builder->writeCSourceFile(options[OUTPUT_FILENAME].value);
|
|
} else {
|
|
builder->writeBinaryFile(options[OUTPUT_FILENAME].value);
|
|
}
|
|
|
|
return errorCode.get();
|
|
|
|
#endif
|
|
}
|
|
|
|
#if !UCONFIG_NO_NORMALIZATION
|
|
|
|
void parseFile(std::ifstream &f, Normalizer2DataBuilder &builder) {
|
|
IcuToolErrorCode errorCode("gennorm2/parseFile()");
|
|
std::string lineString;
|
|
uint32_t startCP, endCP;
|
|
while(std::getline(f, lineString)) {
|
|
if (lineString.empty()) {
|
|
continue; // skip empty lines.
|
|
}
|
|
#if (U_CPLUSPLUS_VERSION >= 11)
|
|
char *line = &lineString.front();
|
|
#else
|
|
char *line = &lineString.at(0);
|
|
#endif
|
|
char *comment=(char *)strchr(line, '#');
|
|
if(comment!=NULL) {
|
|
*comment=0;
|
|
}
|
|
u_rtrim(line);
|
|
if(line[0]==0) {
|
|
continue; // skip empty and comment-only lines
|
|
}
|
|
if(line[0]=='*') {
|
|
const char *s=u_skipWhitespace(line+1);
|
|
if(0==strncmp(s, "Unicode", 7)) {
|
|
s=u_skipWhitespace(s+7);
|
|
builder.setUnicodeVersion(s);
|
|
}
|
|
continue; // reserved syntax
|
|
}
|
|
const char *delimiter;
|
|
int32_t rangeLength=
|
|
u_parseCodePointRangeAnyTerminator(line, &startCP, &endCP, &delimiter, errorCode);
|
|
if(errorCode.isFailure()) {
|
|
fprintf(stderr, "gennorm2 error: parsing code point range from %s\n", line);
|
|
exit(errorCode.reset());
|
|
}
|
|
if (endCP >= 0xd800 && startCP <= 0xdfff) {
|
|
fprintf(stderr, "gennorm2 error: value or mapping for surrogate code points: %s\n",
|
|
line);
|
|
exit(U_ILLEGAL_ARGUMENT_ERROR);
|
|
}
|
|
delimiter=u_skipWhitespace(delimiter);
|
|
if(*delimiter==':') {
|
|
const char *s=u_skipWhitespace(delimiter+1);
|
|
char *end;
|
|
unsigned long value=strtoul(s, &end, 10);
|
|
if(end<=s || *u_skipWhitespace(end)!=0 || value>=0xff) {
|
|
fprintf(stderr, "gennorm2 error: parsing ccc from %s\n", line);
|
|
exit(U_PARSE_ERROR);
|
|
}
|
|
for(UChar32 c=(UChar32)startCP; c<=(UChar32)endCP; ++c) {
|
|
builder.setCC(c, (uint8_t)value);
|
|
}
|
|
continue;
|
|
}
|
|
if(*delimiter=='-') {
|
|
if(*u_skipWhitespace(delimiter+1)!=0) {
|
|
fprintf(stderr, "gennorm2 error: parsing remove-mapping %s\n", line);
|
|
exit(U_PARSE_ERROR);
|
|
}
|
|
for(UChar32 c=(UChar32)startCP; c<=(UChar32)endCP; ++c) {
|
|
builder.removeMapping(c);
|
|
}
|
|
continue;
|
|
}
|
|
if(*delimiter=='=' || *delimiter=='>') {
|
|
UChar uchars[Normalizer2Impl::MAPPING_LENGTH_MASK];
|
|
int32_t length=u_parseString(delimiter+1, uchars, UPRV_LENGTHOF(uchars), NULL, errorCode);
|
|
if(errorCode.isFailure()) {
|
|
fprintf(stderr, "gennorm2 error: parsing mapping string from %s\n", line);
|
|
exit(errorCode.reset());
|
|
}
|
|
UnicodeString mapping(FALSE, uchars, length);
|
|
if(*delimiter=='=') {
|
|
if(rangeLength!=1) {
|
|
fprintf(stderr,
|
|
"gennorm2 error: round-trip mapping for more than 1 code point on %s\n",
|
|
line);
|
|
exit(U_PARSE_ERROR);
|
|
}
|
|
builder.setRoundTripMapping((UChar32)startCP, mapping);
|
|
} else {
|
|
for(UChar32 c=(UChar32)startCP; c<=(UChar32)endCP; ++c) {
|
|
builder.setOneWayMapping(c, mapping);
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
fprintf(stderr, "gennorm2 error: unrecognized data line %s\n", line);
|
|
exit(U_PARSE_ERROR);
|
|
}
|
|
}
|
|
|
|
#endif // !UCONFIG_NO_NORMALIZATION
|
|
|
|
U_NAMESPACE_END
|
|
|
|
/*
|
|
* Hey, Emacs, please set the following:
|
|
*
|
|
* Local Variables:
|
|
* indent-tabs-mode: nil
|
|
* End:
|
|
*
|
|
*/
|