void NS_GetComplexLineBreaks(const PRUnichar* aText, PRUint32 aLength, PRUint8* aBreakBefore) { NS_ASSERTION(aText, "aText shouldn't be null"); TextBreakLocatorRef breakLocator; memset(aBreakBefore, false, aLength * sizeof(PRUint8)); OSStatus status = UCCreateTextBreakLocator(NULL, 0, kUCTextBreakLineMask, &breakLocator); if (status != noErr) return; for (UniCharArrayOffset position = 0; position < aLength;) { UniCharArrayOffset offset; status = UCFindTextBreak(breakLocator, kUCTextBreakLineMask, position == 0 ? kUCTextBreakLeadingEdgeMask : (kUCTextBreakLeadingEdgeMask | kUCTextBreakIterateMask), aText, aLength, position, &offset); if (status != noErr) break; aBreakBefore[offset] = true; position = offset; } UCDisposeTextBreakLocator(&breakLocator); }
int nextBreakablePosition(const UChar* str, int pos, int len, bool treatNoBreakSpaceAsBreak) { #if !PLATFORM(MAC) || !defined(BUILDING_ON_TIGER) TextBreakIterator* breakIterator = 0; #endif int nextBreak = -1; UChar lastCh = pos > 0 ? str[pos - 1] : 0; for (int i = pos; i < len; i++) { UChar ch = str[i]; if (isBreakableSpace(ch, treatNoBreakSpaceAsBreak) || shouldBreakAfter(lastCh)) return i; if (needsLineBreakIterator(ch) || needsLineBreakIterator(lastCh)) { if (nextBreak < i && i) { #if !PLATFORM(MAC) || !defined(BUILDING_ON_TIGER) if (!breakIterator) breakIterator = lineBreakIterator(str, len); if (breakIterator) nextBreak = textBreakFollowing(breakIterator, i - 1); #else static TextBreakLocatorRef breakLocator = lineBreakLocator(); if (breakLocator) { UniCharArrayOffset nextUCBreak; if (UCFindTextBreak(breakLocator, kUCTextBreakLineMask, 0, str, len, i, &nextUCBreak) == 0) nextBreak = nextUCBreak; } #endif } if (i == nextBreak && !isBreakableSpace(lastCh, treatNoBreakSpaceAsBreak)) return i; } lastCh = ch; } return len; }
void doForwardTest() { if (opt_terse == FALSE) { printf("Doing the forward test\n"); } int32_t noBreaks = 0; int32_t i = 0; unsigned long startTime = timeGetTime(); unsigned long elapsedTime = 0; if(opt_icu) { createICUBrkIt(); brkit->setText(text); brkit->first(); if (opt_terse == FALSE) { printf("Warmup\n"); } while(brkit->next() != BreakIterator::DONE) { noBreaks++; } if (opt_terse == FALSE) { printf("Measure\n"); } startTime = timeGetTime(); for(i = 0; i < opt_loopCount; i++) { brkit->first(); while(brkit->next() != BreakIterator::DONE) { } } elapsedTime = timeGetTime()-startTime; } else if(opt_mac) { #ifdef U_DARWIN createMACBrkIt(); UniChar* filePtr = text; OSStatus status = noErr; UniCharCount startOffset = 0, breakOffset = 0, numUniChars = textSize; startOffset = 0; //printf("\t---Search forward--\n"); while (startOffset < numUniChars) { status = UCFindTextBreak(breakRef, macBreakType, kUCTextBreakLeadingEdgeMask, filePtr, numUniChars, startOffset, &breakOffset); //require_action(status == noErr, EXIT, printf( "**UCFindTextBreak failed: startOffset %d, status %d\n", (int)startOffset, (int)status)); //require_action((breakOffset <= numUniChars),EXIT, printf("**UCFindTextBreak breakOffset too big: startOffset %d, breakOffset %d\n", (int)startOffset, (int)breakOffset)); // Output break //printf("\t%d\n", (int)breakOffset); // Increment counters noBreaks++; startOffset = breakOffset; } startTime = timeGetTime(); for(i = 0; i < opt_loopCount; i++) { startOffset = 0; while (startOffset < numUniChars) { status = UCFindTextBreak(breakRef, macBreakType, kUCTextBreakLeadingEdgeMask, filePtr, numUniChars, startOffset, &breakOffset); // Increment counters startOffset = breakOffset; } } elapsedTime = timeGetTime()-startTime; UCDisposeTextBreakLocator(&breakRef); #endif } if (opt_terse == FALSE) { int32_t loopTime = (int)(float(1000) * ((float)elapsedTime/(float)opt_loopCount)); int32_t timePerCU = (int)(float(1000) * ((float)loopTime/(float)textSize)); int32_t timePerBreak = (int)(float(1000) * ((float)loopTime/(float)noBreaks)); printf("forward break iteration average loop time %d\n", loopTime); printf("number of code units %d average time per code unit %d\n", textSize, timePerCU); printf("number of breaks %d average time per break %d\n", noBreaks, timePerBreak); } else { printf("time=%d\nevents=%d\nsize=%d\n", elapsedTime, noBreaks, textSize); } }