TLDR: The effect you are seeing arises because digits and alphabetic characters are treated differently by the Unicode algorithm that determines the rendering of text containing format control characters.
For the texts you are displaying:
- \u202e is the RIGHT-TO-LEFT OVERRIDE (RLO) character.
- \u202c is the POP DIRECTIONAL FORMATTING (PDF) character.
- Both are formatting control characters in Unicode, and their sole effect is to impact the appearance of output text.
- In your examples the RLO character specifies that the text which follows is to be displayed from right to left (RLO), and PDF character cancels ("pops") the effect of the RLO.
That explains why the text 123\u202e987\u202cxyzabc in your example is rendered as 123987xyzabc. The RLO (\u202e) causes the text that follows to be rendered in right to left order (so 987 is displayed as 789), and the PDF (\u202c) terminates reversal for the subsequent text.
But it does not explain why 123\u202e987\u202c456abc is rendered as 123456789abc. By that argument, the expected output should be 123789456abc instead.
The algorithm used to determine the output in scenarios like this is very complex, but one factor is the directionality of the characters being rendered. Alphabetic characters have strong directionality, but numbers (i.e. digit characters) have weak directionality. For full details see the Unicode document Unicode® Standard Annex #9 UNICODE BIDIRECTIONAL ALGORITHM, and especially section 3.3.4 Resolving Weak Types
That document provides an example similar to yours, with text containing a RIGHT-TO-LEFT EMBEDDING (RLE) character (rather than an RLO), later followed by a PDF and some trailing text containing digits:
Memory: it is called "[RLE]AN INTRODUCTION TO java[PDF]" - $19.95 in hardcover.
Display: it is called "$19.95 - "java OT NOITCUDORTNI NA in hardcover.
Note that in their example it wasn't just the digits that were moved. The dollar sign and the period were as well, because all six of the characters in the text $19.95 have weak directionality.
Notes: