question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behavior in PS7 with replacing specific bytes in While-clause

See original GitHub issue

For no other reason than to challenge myself, I tried to make one of my scripts to use as less bytes as possible.

I need to find all double closing brackets and separate them:

while($l.Contains("))")){$l=$l.Replace("))",")`t)")}
# ))  -> )`t)
# ))) -> )`t)`t)

To describe this here I use a TAB to have a visible character. In my code I pasted a [char]7 (that will never be in the input text). This works in 5.1, 6.2 and 7.1

To make my code shorter I used this:

while($l-ne($l=$l.Replace("..",".`t"))){}

This also works in 5, 6 and 7 if you use a TAB (pasted, not `t). With [char]7 it’s OK in Powershell 5 and 6, but fails in 7.

Actual behavior in PS7

$code = '$l = "#####" ; while($l-ne($l=$l.Replace("##","#Q#"))){} ; $l.Split("Q") -join "-"'
1..31 | %{"{0:00} - {1}" -f $_, (Invoke-Expression $code.Replace("Q",[char]$_))}

 1 - #-##-##
 2 - #-##-##
 3 - #-##-##
 4 - #-##-##
 5 - #-##-##
 6 - #-##-##
 7 - #-##-##
 8 - #-##-##
 9 - #-#-#-#-#
10 - #-#-#-#-#
11 - #-#-#-#-#
12 - #-#-#-#-#
13 - #-#-#-#-#
14 - #-##-##
15 - #-##-##
16 - #-##-##
17 - #-##-##
18 - #-##-##
19 - #-##-##
20 - #-##-##
21 - #-##-##
22 - #-##-##
23 - #-##-##
24 - #-##-##
25 - #-##-##
26 - #-##-##
27 - #-##-##
28 - #-##-##
29 - #-##-##
30 - #-##-##
31 - #-##-##

The results of bytes 9 to 13 are as expected. In PS5 or PS6 all bytes result in the correct output.

Why does this behave different for specific values?

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.2
PSEdition                      Core
GitCommitId                    7.1.2
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Mar 7, 2021

I agree, @vexx32 - a cursory web search didn’t provide any clues.

Searching the issues at https://github.com/dotnet/runtime/issues I did find this, which may explain the change (I haven’t dug deeper):

https://github.com/dotnet/runtime/issues/43736#issuecomment-714779040

in .NET 5.0 we have switched to using ICU instead of NLS. You can look at https://docs.microsoft.com/en-us/dotnet/standard/globalization-localization/globalization-icu for more details.

<div> GitHub</div><div>dotnet/runtime</div><div>.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime</div>
<div>Globalization and ICU</div><div>Learn more about: .NET globalization and ICU</div>
0reactions
daxian-dbwcommented, Mar 18, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bash shell scripting: replace n bytes in a specific position ...
I want to replace these characters with spaces in each line of the file. In java I can get the desired result by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found