diff --git a/src/kits/interface/BTextView/TextGapBuffer.cpp b/src/kits/interface/BTextView/TextGapBuffer.cpp index 535ca32fe5..2a2ecd0a89 100644 --- a/src/kits/interface/BTextView/TextGapBuffer.cpp +++ b/src/kits/interface/BTextView/TextGapBuffer.cpp @@ -1,5 +1,5 @@ //------------------------------------------------------------------------------ -// Copyright (c) 2001-2004, OpenBeOS +// Copyright (c) 2001-2004, Haiku, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), @@ -53,7 +53,7 @@ _BTextGapBuffer_::_BTextGapBuffer_() fPasswordMode(false) { fBuffer = (char *)malloc(fExtraCount + fItemCount); - fScratchBuffer = (char*)malloc(0); + fScratchBuffer = NULL; } //------------------------------------------------------------------------------ _BTextGapBuffer_::~_BTextGapBuffer_() @@ -246,10 +246,50 @@ _BTextGapBuffer_::Text() return fText; }*/ //------------------------------------------------------------------------------ +void +_BTextGapBuffer_::GetString(int32 offset, int32 length, char *buffer) +{ + if (buffer == NULL) + return; + + int32 textLen = Length(); + + if (offset < 0 || offset > (textLen - 1) || length < 1) { + buffer[0] = '\0'; + return; + } + + length = ((offset + length) > textLen) ? textLen - offset : length; + + bool isStartBeforeGap = (offset < fGapIndex); + bool isEndBeforeGap = ((offset + length - 1) < fGapIndex); + + if (isStartBeforeGap == isEndBeforeGap) { + char *source = fBuffer + offset; + if (!isStartBeforeGap) + source += fGapCount; + + memcpy(buffer, source, length); + + } else { + // if we are here, it can only be that start is before gap, + // and the end is after gap. + + int32 beforeLen = fGapIndex - offset; + int32 afterLen = length - beforeLen; + + memcpy(buffer, fBuffer + offset, beforeLen); + memcpy(buffer + beforeLen, fBuffer + fGapIndex, afterLen); + + } + + buffer[length] = '\0'; +} +//------------------------------------------------------------------------------ char _BTextGapBuffer_::RealCharAt(int32 offset) const { - return *(fBuffer + offset); + return (offset < fGapIndex) ? fBuffer[offset] : fBuffer[offset + fGapCount]; } //------------------------------------------------------------------------------ bool diff --git a/src/kits/interface/BTextView/TextGapBuffer.h b/src/kits/interface/BTextView/TextGapBuffer.h index e4d7c451d3..5eacc4b527 100644 --- a/src/kits/interface/BTextView/TextGapBuffer.h +++ b/src/kits/interface/BTextView/TextGapBuffer.h @@ -63,7 +63,7 @@ virtual ~_BTextGapBuffer_(); // char *RealText(); -// void GetString(int32 offset, int32 length, char *buffer); + void GetString(int32 offset, int32 length, char *buffer); // void GetString(int32, int32 *); char RealCharAt(int32 offset) const; diff --git a/src/kits/interface/BTextView/TextView.cpp b/src/kits/interface/BTextView/TextView.cpp index 0dc1d5343c..6c039a5c4f 100644 --- a/src/kits/interface/BTextView/TextView.cpp +++ b/src/kits/interface/BTextView/TextView.cpp @@ -1230,7 +1230,8 @@ BTextView::TextLength() const CALLED(); return fText->Length(); } -//------------------------------------------------------------------------------ + + void BTextView::GetText(int32 offset, int32 length, char *buffer) const { @@ -1238,16 +1239,7 @@ BTextView::GetText(int32 offset, int32 length, char *buffer) const if (buffer == NULL) return; - int32 textLen = fText->Length(); - if (offset < 0 || offset > (textLen - 1)) { - buffer[0] = '\0'; - return; - } - - length = ((offset + length) > textLen) ? textLen - offset : length; - for (int32 i = 0; i < length; i++) - buffer[i] = (*fText)[i + offset]; - buffer[length] = '\0'; + fText->GetString(offset, length, buffer); }