diff options
| author | Neil <nyamatongwe@gmail.com> | 2022-07-30 09:46:03 +1000 | 
|---|---|---|
| committer | Neil <nyamatongwe@gmail.com> | 2022-07-30 09:46:03 +1000 | 
| commit | 83d8151122916867367b75ac38d0cf32ba0ac932 (patch) | |
| tree | cd564f78d4b66842856856c8778a47fb43098f03 /src | |
| parent | 440bc7509db77ef88326f6c7d98395bc970f3b6f (diff) | |
| download | scintilla-mirror-83d8151122916867367b75ac38d0cf32ba0ac932.tar.gz | |
Add cast functions to simplify code and avoid warnings.
Change targetAct type to avoid type widening warnings.
Diffstat (limited to 'src')
| -rw-r--r-- | src/CellBuffer.cxx | 65 | 
1 files changed, 41 insertions, 24 deletions
| diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx index a2cee4fcf..2a3aed146 100644 --- a/src/CellBuffer.cxx +++ b/src/CellBuffer.cxx @@ -91,6 +91,11 @@ using namespace Scintilla::Internal;  template <typename POS>  class LineStartIndex { +	// line_cast(): cast Sci::Line to either 32-bit or 64-bit value +	// This avoids warnings from Visual C++ Code Analysis and shortens code +	static constexpr POS line_cast(Sci::Line pos) noexcept { +		return static_cast<POS>(pos); +	}  public:  	int refCount;  	Partitioning<POS> starts; @@ -104,7 +109,7 @@ public:  		for (Sci::Line line = starts.Partitions(); line < lines; line++) {  			// Produce an ascending sequence that will be filled in with correct widths later  			length++; -			starts.InsertPartition(static_cast<POS>(line), static_cast<POS>(length)); +			starts.InsertPartition(line_cast(line), line_cast(length));  		}  		return refCount == 1;  	} @@ -119,12 +124,12 @@ public:  		return refCount > 0;  	}  	Sci::Position LineWidth(Sci::Line line) const noexcept { -		return starts.PositionFromPartition(static_cast<POS>(line) + 1) - -			starts.PositionFromPartition(static_cast<POS>(line)); +		return starts.PositionFromPartition(line_cast(line) + 1) - +			starts.PositionFromPartition(line_cast(line));  	}  	void SetLineWidth(Sci::Line line, Sci::Position width) noexcept {  		const Sci::Position widthCurrent = LineWidth(line); -		starts.InsertText(static_cast<POS>(line), static_cast<POS>(width - widthCurrent)); +		starts.InsertText(line_cast(line), line_cast(width - widthCurrent));  	}  	void AllocateLines(Sci::Line lines) {  		if (lines > starts.Partitions()) { @@ -134,9 +139,9 @@ public:  	void InsertLines(Sci::Line line, Sci::Line lines) {  		// Insert multiple lines with each temporarily 1 character wide.  		// The line widths will be fixed up by later measuring code. -		const POS lineAsPos = static_cast<POS>(line); +		const POS lineAsPos = line_cast(line);  		const POS lineStart = starts.PositionFromPartition(lineAsPos - 1) + 1; -		for (POS l = 0; l < static_cast<POS>(lines); l++) { +		for (POS l = 0; l < line_cast(lines); l++) {  			starts.InsertPartition(lineAsPos + l, lineStart + l);  		}  	} @@ -156,6 +161,18 @@ class LineVector : public ILineVector {  			| (startsUTF16.Active() ? LineCharacterIndexType::Utf16 : LineCharacterIndexType::None);  	} +	// pos_cast(): cast Sci::Line and Sci::Position to either 32-bit or 64-bit value +	// This avoids warnings from Visual C++ Code Analysis and shortens code +	static constexpr POS pos_cast(Sci::Position pos) noexcept { +		return static_cast<POS>(pos); +	} + +	// line_from_pos_cast(): return 32-bit or 64-bit value as Sci::Line  +	// This avoids warnings from Visual C++ Code Analysis and shortens code +	static constexpr Sci::Line line_from_pos_cast(POS line) noexcept { +		return static_cast<Sci::Line>(line); +	} +  public:  	LineVector() : starts(256), perLine(nullptr), activeIndices(LineCharacterIndexType::None) {  	} @@ -171,11 +188,11 @@ public:  		perLine = pl;  	}  	void InsertText(Sci::Line line, Sci::Position delta) noexcept override { -		starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta)); +		starts.InsertText(pos_cast(line), pos_cast(delta));  	}  	void InsertLine(Sci::Line line, Sci::Position position, bool lineStart) override { -		const POS lineAsPos = static_cast<POS>(line); -		starts.InsertPartition(lineAsPos, static_cast<POS>(position)); +		const POS lineAsPos = pos_cast(line); +		starts.InsertPartition(lineAsPos, pos_cast(position));  		if (activeIndices != LineCharacterIndexType::None) {  			if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) {  				startsUTF32.InsertLines(line, 1); @@ -191,7 +208,7 @@ public:  		}  	}  	void InsertLines(Sci::Line line, const Sci::Position *positions, size_t lines, bool lineStart) override { -		const POS lineAsPos = static_cast<POS>(line); +		const POS lineAsPos = pos_cast(line);  		if constexpr (sizeof(Sci::Position) == sizeof(POS)) {  			starts.InsertPartitions(lineAsPos, positions, lines);  		} else { @@ -212,22 +229,22 @@ public:  		}  	}  	void SetLineStart(Sci::Line line, Sci::Position position) noexcept override { -		starts.SetPartitionStartPosition(static_cast<POS>(line), static_cast<POS>(position)); +		starts.SetPartitionStartPosition(pos_cast(line), pos_cast(position));  	}  	void RemoveLine(Sci::Line line) override { -		starts.RemovePartition(static_cast<POS>(line)); +		starts.RemovePartition(pos_cast(line));  		if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { -			startsUTF32.starts.RemovePartition(static_cast<POS>(line)); +			startsUTF32.starts.RemovePartition(pos_cast(line));  		}  		if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { -			startsUTF16.starts.RemovePartition(static_cast<POS>(line)); +			startsUTF16.starts.RemovePartition(pos_cast(line));  		}  		if (perLine) {  			perLine->RemoveLine(line);  		}  	}  	Sci::Line Lines() const noexcept override { -		return static_cast<Sci::Line>(starts.Partitions()); +		return line_from_pos_cast(starts.Partitions());  	}  	void AllocateLines(Sci::Line lines) override {  		if (lines > Lines()) { @@ -241,17 +258,17 @@ public:  		}  	}  	Sci::Line LineFromPosition(Sci::Position pos) const noexcept override { -		return static_cast<Sci::Line>(starts.PartitionFromPosition(static_cast<POS>(pos))); +		return line_from_pos_cast(starts.PartitionFromPosition(pos_cast(pos)));  	}  	Sci::Position LineStart(Sci::Line line) const noexcept override { -		return starts.PositionFromPartition(static_cast<POS>(line)); +		return starts.PositionFromPartition(pos_cast(line));  	}  	void InsertCharacters(Sci::Line line, CountWidths delta) noexcept override {  		if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { -			startsUTF32.starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta.WidthUTF32())); +			startsUTF32.starts.InsertText(pos_cast(line), pos_cast(delta.WidthUTF32()));  		}  		if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { -			startsUTF16.starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta.WidthUTF16())); +			startsUTF16.starts.InsertText(pos_cast(line), pos_cast(delta.WidthUTF16()));  		}  	}  	void SetLineCharactersWidth(Sci::Line line, CountWidths width) noexcept override { @@ -294,16 +311,16 @@ public:  	}  	Sci::Position IndexLineStart(Sci::Line line, LineCharacterIndexType lineCharacterIndex) const noexcept override {  		if (lineCharacterIndex == LineCharacterIndexType::Utf32) { -			return startsUTF32.starts.PositionFromPartition(static_cast<POS>(line)); +			return startsUTF32.starts.PositionFromPartition(pos_cast(line));  		} else { -			return startsUTF16.starts.PositionFromPartition(static_cast<POS>(line)); +			return startsUTF16.starts.PositionFromPartition(pos_cast(line));  		}  	}  	Sci::Line LineFromPositionIndex(Sci::Position pos, LineCharacterIndexType lineCharacterIndex) const noexcept override {  		if (lineCharacterIndex == LineCharacterIndexType::Utf32) { -			return static_cast<Sci::Line>(startsUTF32.starts.PartitionFromPosition(static_cast<POS>(pos))); +			return line_from_pos_cast(startsUTF32.starts.PartitionFromPosition(pos_cast(pos)));  		} else { -			return static_cast<Sci::Line>(startsUTF16.starts.PartitionFromPosition(static_cast<POS>(pos))); +			return line_from_pos_cast(startsUTF16.starts.PartitionFromPosition(pos_cast(pos)));  		}  	}  }; @@ -384,7 +401,7 @@ const char *UndoHistory::AppendAction(ActionType at, Sci::Position position, con  	if (currentAction >= 1) {  		if (0 == undoSequenceDepth) {  			// Top level actions may not always be coalesced -			int targetAct = -1; +			ptrdiff_t targetAct = -1;  			const Action *actPrevious = &(actions[currentAction + targetAct]);  			// Container actions may forward the coalesce state of Scintilla Actions.  			while ((actPrevious->at == ActionType::container) && actPrevious->mayCoalesce) { | 
