aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKeisuke Kuroyanagi <ksk@google.com>2014-07-25 02:03:56 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2014-07-25 02:03:56 +0000
commit6bd15b8662e3d8b51383f5d78205a4cd87a65b65 (patch)
tree3cd8f0ceb063a8e22fbf4098498ea20a6d2e6878
parent5d44dfe005a615db689ddc1851d4855a8a44c6b8 (diff)
parente32cb6f8f267ce044c2880106e5a5a309ec9dc33 (diff)
downloadlatinime-6bd15b8662e3d8b51383f5d78205a4cd87a65b65.tar.gz
latinime-6bd15b8662e3d8b51383f5d78205a4cd87a65b65.tar.xz
latinime-6bd15b8662e3d8b51383f5d78205a4cd87a65b65.zip
am e32cb6f8: am 16665da2: am f78eb27c: Merge "Refactoring checkAndPrepareWriting." into lmp-dev
* commit 'e32cb6f8f267ce044c2880106e5a5a309ec9dc33': Refactoring checkAndPrepareWriting.
-rw-r--r--native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp46
1 files changed, 28 insertions, 18 deletions
diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp
index 259dae4c6..6e88bed9e 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.cpp
@@ -107,27 +107,37 @@ bool BufferWithExtendableBuffer::extendBuffer() {
}
bool BufferWithExtendableBuffer::checkAndPrepareWriting(const int pos, const int size) {
- if (isInAdditionalBuffer(pos)) {
- const int tailPosition = getTailPosition();
- if (pos == tailPosition) {
- // Append data to the tail.
- if (pos + size > static_cast<int>(mAdditionalBuffer.size()) + mOriginalBufferSize) {
- // Need to extend buffer.
- if (!extendBuffer()) {
- return false;
- }
- }
- mUsedAdditionalBufferSize += size;
- } else if (pos + size > tailPosition) {
- // The access will beyond the tail of used region.
- return false;
- }
- } else {
- if (pos < 0 || mOriginalBufferSize < pos + size) {
- // Invalid position or violate the boundary.
+ if (pos < 0 || size < 0) {
+ // Invalid position or size.
+ return false;
+ }
+ const size_t totalRequiredSize = static_cast<size_t>(pos + size);
+ if (!isInAdditionalBuffer(pos)) {
+ // Here don't need to care about the additional buffer.
+ if (static_cast<size_t>(mOriginalBufferSize) < totalRequiredSize) {
+ // Violate the boundary.
return false;
}
+ // The buffer has sufficient capacity.
+ return true;
+ }
+ // Hereafter, pos is in the additional buffer.
+ const size_t tailPosition = static_cast<size_t>(getTailPosition());
+ if (totalRequiredSize <= tailPosition) {
+ // The buffer has sufficient capacity.
+ return true;
+ }
+ if (static_cast<size_t>(pos) != tailPosition) {
+ // The additional buffer must be extended from the tail position.
+ return false;
+ }
+ const size_t extendSize = totalRequiredSize -
+ std::min(mAdditionalBuffer.size() + mOriginalBufferSize, totalRequiredSize);
+ if (extendSize > 0 && !extendBuffer()) {
+ // Failed to extend the buffer.
+ return false;
}
+ mUsedAdditionalBufferSize += size;
return true;
}