diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FragmentedTextDocument.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FragmentedTextDocument.java index 009fbbd30b..d92404eeb4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FragmentedTextDocument.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/FragmentedTextDocument.java @@ -76,14 +76,13 @@ final class FragmentedTextDocument extends BaseMappedDocument implements TextDoc lastAccessedFragment = f; } - if (!inclusive && f.outEnd() == outOffset) { - if (f.next != null) { + if (inclusive && f.outEnd() == outOffset && f.next != null) { + // Inclusive means, the offset must correspond to a character in the source document. + // Here we have to skip forward to the fragment that contains the character, because + // it's not this one. + do { f = f.next; - lastAccessedFragment = f; - // fallthrough - } else { - return f.outToIn(outOffset) + 1; - } + } while (f.next != null && f.outLen() == 0); } return f.outToIn(outOffset); } diff --git a/pmd-cpp/src/test/resources/net/sourceforge/pmd/lang/cpp/cpd/testdata/continuation_intra_token.txt b/pmd-cpp/src/test/resources/net/sourceforge/pmd/lang/cpp/cpd/testdata/continuation_intra_token.txt index febb035697..df9e43234b 100644 --- a/pmd-cpp/src/test/resources/net/sourceforge/pmd/lang/cpp/cpd/testdata/continuation_intra_token.txt +++ b/pmd-cpp/src/test/resources/net/sourceforge/pmd/lang/cpp/cpd/testdata/continuation_intra_token.txt @@ -1,14 +1,14 @@ [Image] or [Truncated image[ Bcol Ecol L1 [void] 1 2 -L5 - [main] 2 2 -L9 - [(] 2 2 +L6 + [main] 1 2 L10 - [)] 2 2 -L12 - [{] 2 2 -L14 - [}] 2 2 + [(] 1 2 +L11 + [)] 1 2 +L13 + [{] 1 2 +L15 + [}] 1 2 EOF