Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utf8Array] AsSpanメソッドで初期インデックスが指定されている場合、範囲外になっていた問題を修正 #78

Merged
merged 1 commit into from
Nov 5, 2021

Conversation

finphie
Copy link
Owner

@finphie finphie commented Nov 5, 2021

SharpLab

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class C {
    readonly byte[] _x;
    
    public C(byte[] x) => _x = x;
    
    public ReadOnlySpan<byte> B1(int start) => _x.AsSpan(start);
    
    public ReadOnlySpan<byte> B2(int start)
    {
        ref var xs = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_x), (nint)(uint)start);
        return MemoryMarshal.CreateReadOnlySpan(ref xs, _x.Length - start);
    }
    
    public ReadOnlySpan<byte> B3(int start)
    {
        var length = _x.Length - start;
        ref var xs = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_x), (nint)(uint)start);
        return MemoryMarshal.CreateReadOnlySpan(ref xs, length);
    }
    
    public ReadOnlySpan<byte> B4(int start)
    {
        var span = new ReadOnlySpan<byte>(_x, start, _x.Length - start);
        return span;
    }
    
    public ReadOnlySpan<byte> B5(int start)
    {
        var length = _x.Length - start;
        var span = new ReadOnlySpan<byte>(_x, start, length);
        return span;
    }
}
C.B1(Int32)
    L0000: sub rsp, 0x28
    L0004: mov rax, [rcx+8]
    L0008: test rax, rax
    L000b: jne short L0019
    L000d: test r8d, r8d
    L0010: jne short L003e
    L0012: xor ecx, ecx
    L0014: xor r9d, r9d
    L0017: jmp short L002f
    L0019: mov r9d, [rax+8]
    L001d: cmp r9d, r8d
    L0020: jb short L003e
    L0022: add rax, 0x10
    L0026: sub r9d, r8d
    L0029: movsxd rcx, r8d
    L002c: add rcx, rax
    L002f: mov [rdx], rcx
    L0032: mov [rdx+8], r9d
    L0036: mov rax, rdx
    L0039: add rsp, 0x28
    L003d: ret
    L003e: call 0x00007ffc8f6d5ce8
    L0043: int3

C.B2(Int32)
    L0000: mov rax, [rcx+8]
    L0004: mov rcx, rax
    L0007: cmp [rcx], ecx
    L0009: add rcx, 0x10
    L000d: mov eax, [rax+8]
    L0010: sub eax, r8d
    L0013: mov r8d, r8d
    L0016: add rcx, r8
    L0019: mov [rdx], rcx
    L001c: mov [rdx+8], eax
    L001f: mov rax, rdx
    L0022: ret

C.B3(Int32)
    L0000: mov rax, [rcx+8]
    L0004: mov ecx, [rax+8]
    L0007: sub ecx, r8d
    L000a: cmp [rax], eax
    L000c: add rax, 0x10
    L0010: mov r8d, r8d
    L0013: add rax, r8
    L0016: mov [rdx], rax
    L0019: mov [rdx+8], ecx
    L001c: mov rax, rdx
    L001f: ret

C.B4(Int32)
    L0000: sub rsp, 0x28
    L0004: mov rax, [rcx+8]
    L0008: mov rcx, rax
    L000b: mov eax, [rax+8]
    L000e: sub eax, r8d
    L0011: mov r9d, r8d
    L0014: mov r10d, eax
    L0017: add r9, r10
    L001a: mov r10d, [rcx+8]
    L001e: cmp r9, r10
    L0021: ja short L003b
    L0023: add rcx, 0x10
    L0027: movsxd r8, r8d
    L002a: add rcx, r8
    L002d: mov [rdx], rcx
    L0030: mov [rdx+8], eax
    L0033: mov rax, rdx
    L0036: add rsp, 0x28
    L003a: ret
    L003b: call 0x00007ffc8f6d5ce8
    L0040: int3

C.B5(Int32)
    L0000: sub rsp, 0x28
    L0004: mov rax, [rcx+8]
    L0008: mov ecx, [rax+8]
    L000b: sub ecx, r8d
    L000e: test rax, rax
    L0011: jne short L0020
    L0013: or r8d, ecx
    L0016: jne short L004e
    L0018: xor r9d, r9d
    L001b: xor r10d, r10d
    L001e: jmp short L003f
    L0020: mov r9d, r8d
    L0023: mov r10d, ecx
    L0026: add r9, r10
    L0029: mov r10d, [rax+8]
    L002d: cmp r9, r10
    L0030: ja short L004e
    L0032: add rax, 0x10
    L0036: movsxd r9, r8d
    L0039: add r9, rax
    L003c: mov r10d, ecx
    L003f: mov [rdx], r9
    L0042: mov [rdx+8], r10d
    L0046: mov rax, rdx
    L0049: add rsp, 0x28
    L004d: ret
    L004e: call 0x00007ffc8f6d5ce8
    L0053: int3

@github-actions github-actions bot added the fix label Nov 5, 2021
@finphie finphie merged commit ddc4957 into main Nov 5, 2021
@finphie finphie deleted the fix/AsSpan branch November 5, 2021 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant