2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2008-05-15 04:54:19 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2008-2015, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2008-05-15 04:54:19 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 05/11/2008 Andy Heninger Port from Java
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2008-06-11 21:52:29 +00:00
|
|
|
#if !UCONFIG_NO_TRANSLITERATION && !UCONFIG_NO_BREAK_ITERATION
|
2008-05-15 04:54:19 +00:00
|
|
|
|
2015-04-29 23:18:32 +00:00
|
|
|
#include "unicode/brkiter.h"
|
|
|
|
#include "unicode/localpointer.h"
|
2008-05-15 04:54:19 +00:00
|
|
|
#include "unicode/uchar.h"
|
2015-04-29 23:18:32 +00:00
|
|
|
#include "unicode/unifilt.h"
|
2008-05-15 04:54:19 +00:00
|
|
|
#include "unicode/uniset.h"
|
2015-04-29 23:18:32 +00:00
|
|
|
|
2008-05-15 04:54:19 +00:00
|
|
|
#include "brktrans.h"
|
|
|
|
#include "cmemory.h"
|
2015-04-29 23:18:32 +00:00
|
|
|
#include "mutex.h"
|
2008-05-15 04:54:19 +00:00
|
|
|
#include "uprops.h"
|
|
|
|
#include "uinvchar.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "uvectr32.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(BreakTransliterator)
|
|
|
|
|
|
|
|
static const UChar SPACE = 32; // ' '
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator with the default delimiters '{' and
|
|
|
|
* '}'.
|
|
|
|
*/
|
|
|
|
BreakTransliterator::BreakTransliterator(UnicodeFilter* adoptedFilter) :
|
2015-04-29 23:18:32 +00:00
|
|
|
Transliterator(UNICODE_STRING("Any-BreakInternal", 17), adoptedFilter),
|
|
|
|
cachedBI(NULL), cachedBoundaries(NULL), fInsertion(SPACE) {
|
2008-05-15 04:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
BreakTransliterator::~BreakTransliterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
BreakTransliterator::BreakTransliterator(const BreakTransliterator& o) :
|
2015-04-29 23:18:32 +00:00
|
|
|
Transliterator(o), cachedBI(NULL), cachedBoundaries(NULL), fInsertion(o.fInsertion) {
|
2015-04-15 22:43:15 +00:00
|
|
|
}
|
2008-05-15 04:54:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* BreakTransliterator::clone(void) const {
|
|
|
|
return new BreakTransliterator(*this);
|
2008-05-15 05:11:08 +00:00
|
|
|
}
|
2008-05-15 04:54:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
|
|
|
*/
|
|
|
|
void BreakTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
|
|
|
|
UBool isIncremental ) const {
|
|
|
|
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2015-04-29 23:18:32 +00:00
|
|
|
LocalPointer<BreakIterator> bi;
|
|
|
|
LocalPointer<UVector32> boundaries;
|
2015-04-15 22:43:15 +00:00
|
|
|
|
2015-04-29 23:18:32 +00:00
|
|
|
{
|
|
|
|
Mutex m;
|
2015-04-15 22:43:15 +00:00
|
|
|
BreakTransliterator *nonConstThis = const_cast<BreakTransliterator *>(this);
|
2015-05-28 17:03:45 +00:00
|
|
|
boundaries.moveFrom(nonConstThis->cachedBoundaries);
|
|
|
|
bi.moveFrom(nonConstThis->cachedBI);
|
2015-04-29 23:18:32 +00:00
|
|
|
}
|
|
|
|
if (bi.isNull()) {
|
|
|
|
bi.adoptInstead(BreakIterator::createWordInstance(Locale::getEnglish(), status));
|
|
|
|
}
|
|
|
|
if (boundaries.isNull()) {
|
|
|
|
boundaries.adoptInstead(new UVector32(status));
|
2015-04-15 22:43:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 23:18:32 +00:00
|
|
|
if (bi.isNull() || boundaries.isNull() || U_FAILURE(status)) {
|
2015-04-15 22:43:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-29 23:18:32 +00:00
|
|
|
|
2008-05-15 04:54:19 +00:00
|
|
|
boundaries->removeAllElements();
|
|
|
|
UnicodeString sText = replaceableAsString(text);
|
|
|
|
bi->setText(sText);
|
|
|
|
bi->preceding(offsets.start);
|
|
|
|
|
|
|
|
// To make things much easier, we will stack the boundaries, and then insert at the end.
|
|
|
|
// generally, we won't need too many, since we will be filtered.
|
|
|
|
|
|
|
|
int32_t boundary;
|
|
|
|
for(boundary = bi->next(); boundary != UBRK_DONE && boundary < offsets.limit; boundary = bi->next()) {
|
|
|
|
if (boundary == 0) continue;
|
|
|
|
// HACK: Check to see that preceeding item was a letter
|
|
|
|
|
|
|
|
UChar32 cp = sText.char32At(boundary-1);
|
|
|
|
int type = u_charType(cp);
|
|
|
|
//System.out.println(Integer.toString(cp,16) + " (before): " + type);
|
|
|
|
if ((U_MASK(type) & (U_GC_L_MASK | U_GC_M_MASK)) == 0) continue;
|
|
|
|
|
|
|
|
cp = sText.char32At(boundary);
|
|
|
|
type = u_charType(cp);
|
|
|
|
//System.out.println(Integer.toString(cp,16) + " (after): " + type);
|
|
|
|
if ((U_MASK(type) & (U_GC_L_MASK | U_GC_M_MASK)) == 0) continue;
|
|
|
|
|
|
|
|
boundaries->addElement(boundary, status);
|
2008-08-01 01:51:21 +00:00
|
|
|
// printf("Boundary at %d\n", boundary);
|
2008-05-15 04:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int delta = 0;
|
|
|
|
int lastBoundary = 0;
|
|
|
|
|
|
|
|
if (boundaries->size() != 0) { // if we found something, adjust
|
|
|
|
delta = boundaries->size() * fInsertion.length();
|
|
|
|
lastBoundary = boundaries->lastElementi();
|
|
|
|
|
|
|
|
// we do this from the end backwards, so that we don't have to keep updating.
|
|
|
|
|
|
|
|
while (boundaries->size() > 0) {
|
|
|
|
boundary = boundaries->popi();
|
|
|
|
text.handleReplaceBetween(boundary, boundary, fInsertion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now fix up the return values
|
|
|
|
offsets.contextLimit += delta;
|
|
|
|
offsets.limit += delta;
|
|
|
|
offsets.start = isIncremental ? lastBoundary + delta : offsets.limit;
|
|
|
|
|
2015-04-15 22:43:15 +00:00
|
|
|
// Return break iterator & boundaries vector to the cache.
|
2015-04-29 23:18:32 +00:00
|
|
|
{
|
|
|
|
Mutex m;
|
|
|
|
BreakTransliterator *nonConstThis = const_cast<BreakTransliterator *>(this);
|
|
|
|
if (nonConstThis->cachedBI.isNull()) {
|
2015-05-28 17:03:45 +00:00
|
|
|
nonConstThis->cachedBI.moveFrom(bi);
|
2015-04-29 23:18:32 +00:00
|
|
|
}
|
|
|
|
if (nonConstThis->cachedBoundaries.isNull()) {
|
2015-05-28 17:03:45 +00:00
|
|
|
nonConstThis->cachedBoundaries.moveFrom(boundaries);
|
2015-04-29 23:18:32 +00:00
|
|
|
}
|
2015-04-15 22:43:15 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 04:54:19 +00:00
|
|
|
// TODO: do something with U_FAILURE(status);
|
|
|
|
// (need to look at transliterators overall, not just here.)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// getInsertion()
|
|
|
|
//
|
|
|
|
const UnicodeString &BreakTransliterator::getInsertion() const {
|
|
|
|
return fInsertion;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// setInsertion()
|
|
|
|
//
|
2009-09-25 17:16:39 +00:00
|
|
|
void BreakTransliterator::setInsertion(const UnicodeString &insertion) {
|
2008-05-15 04:54:19 +00:00
|
|
|
this->fInsertion = insertion;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// replaceableAsString Hack to let break iterators work
|
|
|
|
// on the replaceable text from transliterators.
|
|
|
|
// In practice, the only real Replaceable type that we
|
|
|
|
// will be seeing is UnicodeString, so this function
|
|
|
|
// will normally be efficient.
|
|
|
|
//
|
|
|
|
UnicodeString BreakTransliterator::replaceableAsString(Replaceable &r) {
|
|
|
|
UnicodeString s;
|
2010-05-19 17:29:33 +00:00
|
|
|
UnicodeString *rs = dynamic_cast<UnicodeString *>(&r);
|
|
|
|
if (rs != NULL) {
|
|
|
|
s = *rs;
|
|
|
|
} else {
|
|
|
|
r.extractBetween(0, r.length(), s);
|
|
|
|
}
|
2008-05-15 04:54:19 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|