locked
Function set to return a string is received as string[] - why ? RRS feed

  • Question

  • (Sorry about the title  - not very accurate.)

    This works perfectly in C# but I am having trouble replicating the exact required behaviour in PowerShell. I am unclear as to what I am doing wrong...

    The outline is that a file is passed to a function along with tags. The text between the tags is concatenated and then hashed. So the exact mechanism in use is hyper important, since a since bit error will cause a hash comparison error further down the line. (Detail superfluous to this question).

    # The Test Data
    
    #region codeblock
    $CodeBlock=@'
    A
    '@#/CodeBlockEnd
    #endregion
    #region codeblock
    $ProgramCode=@'
    B
    '@#/ProgramCodeEnd
    #endregion
    
    # The code Reading it.
    
    Function Test
    {
        param
        (
            [string]$filename,
            [string]$tag1,
            [string]$tag2,
            [string]$tag3,
            [string]$tag4
        )
        
        function GetStrBetweenTags
        {
            [OutputType([string])]
            param
            (
                [string]$value, 
                [string]$startTag,
                [string]$endTag
            )
            
            if (($value.Contains($startTag)) -and ($value.Contains($endTag)))
            {
                $index = $value.IndexOf($startTag) + $startTag.Length
                
                [Text.Stringbuilder]$sb = New-Object Text.StringBuilder
                
                [String]$FoundText = $value.Substring($index, $value.IndexOf($endTag) - $index)
    
                [IO.StringReader]$sr = new-object System.IO.StringReader($FoundText)
    
                while (($line = $sr.Readline()) -ne $null)
                {
                    $sb.Append($line)
                }
                [string]$RetStr = $sb.ToString()
                
                Write-host "The Output of GetStrBetweenTags"
                write-host $RetStr
    
                return $RetStr
            }
            else
            {
                return null;
            }
        }
    
    
        [string]$a = GetStrBetweenTags $([io.file]::ReadAllText($filename,[Text.Encoding]::UTF8)) $tag1 $tag2
        Write-host "The Return of GetStrBetweenTags"
        Write-host $a
        [string]$b = GetStrBetweenTags $([io.file]::ReadAllText($filename,[Text.Encoding]::UTF8)) $tag3 $tag4
        Write-host "The Return of GetStrBetweenTags"
        Write-host $b
        [string]$c = [string]::Concat($a,$b)
      }
    Test 'C:\Test.ps1' ('$CodeBlock=@{0}' -f "'") ('{0}@#/CodeBlockEnd' -f "'") ('$ProgramCode=@{0}' -f "'") ('{0}@#/ProgramCodeEnd' -f "'")
    exit
    
    
    The output I am seeing.
    
    The Output of GetStrBetweenTags
    A
    The Return of GetStrBetweenTags
    A A A
    The Output of GetStrBetweenTags
    B
    The Return of GetStrBetweenTags
    B B B
    DZuiowp6Gg0Sx0XIsdBESJC0rDOq0micpLpJoUKu8JUkM/KGD/kSGBK3+5Pj02/lC+KvfGLD7H4gAD/tpN8eZQ==




    • Edited by Anthony Guyon Friday, February 8, 2019 4:51 AM Removed Extraneous code to question
    Friday, February 8, 2019 4:35 AM

Answers

  • Because it has line breaks in it.  "Append" is appending lines because you are using Readline().

    If you would format your code correctly and so it is readable you would be able to see what is happening.  You can also step through it with the debugger.


    \_(ツ)_/

    • Marked as answer by Anthony Guyon Friday, February 8, 2019 5:02 AM
    Friday, February 8, 2019 4:51 AM

All replies

  • Because it has line breaks in it.  "Append" is appending lines because you are using Readline().

    If you would format your code correctly and so it is readable you would be able to see what is happening.  You can also step through it with the debugger.


    \_(ツ)_/

    • Marked as answer by Anthony Guyon Friday, February 8, 2019 5:02 AM
    Friday, February 8, 2019 4:51 AM
  • Thank you... I misunderstood in that case (because in Visual Studio that is not happening). I was using Readline() because I thought (observed) it would not return \r\n - but what your saying is append is adding these? What should i be using to get a continuous string with no line breaks?

    Edit - I read the stingbuilder class and I guess I should be using AppendJoin with an empty character specified as the separator.

    Friday, February 8, 2019 4:57 AM
  • Why are you not just using Get-Content. Do not try to convert C# to PowerShell without q lot more experience with PowerShell.  There is also no need to use the StringBuilder.  PowerShell handles all of that for you.


    \_(ツ)_/


    • Edited by jrv Friday, February 8, 2019 5:18 AM
    Friday, February 8, 2019 5:17 AM
  • This is all you need to generate a hash.

    $f = Get-Item test.txt
    $sr = $f.OpenRead()
    $sha512 = [System.Security.Cryptography.SHA512Managed]::New()
    $hash = $sha512.ComputeHash($sr)

    You can use a memory stream to turn any string into a stream.

    You can use RegEx to extract components of text usually in one line of code.


    \_(ツ)_/

    Friday, February 8, 2019 5:25 AM
  • I got it working with the following adjustments. You got me on the right track by showing me that under thinking is better in PowerShell. Thanks for that.

    [string]$Ret = @()
    while (($line = $sr.ReadLine()) -ne $null)
    {
        if (-not [string]::IsNullOrEmpty($line))
        {
            $Ret += $line
        }
    }

    Friday, February 8, 2019 6:02 AM