|
| 1 | +{$DEFINE SRLT_CONVERSIONS_INCLUDED} |
| 2 | +{$IFNDEF SRLT_UTILS} |
| 3 | + {$I SRLT/utils.simba} |
| 4 | +{$ENDIF} |
| 5 | + |
| 6 | + |
| 7 | +function TStringArray.ToBytes(): TByteArray; |
| 8 | +begin |
| 9 | + Result := Self.Join(#0).ToBytes(); |
| 10 | +end; |
| 11 | + |
| 12 | +function TStringArray.CreateFromBytes(bytes: TByteArray): TStringArray; static; |
| 13 | +var |
| 14 | + str: String; |
| 15 | + len: Integer; |
| 16 | +begin |
| 17 | + if Length(bytes) = 0 then Exit; |
| 18 | + len := Length(bytes); |
| 19 | + |
| 20 | + SetLength(str, len); |
| 21 | + Move(bytes[0], str[1], len); |
| 22 | + |
| 23 | + Result := str.Split(#0, False); |
| 24 | + if str.EndsWith(#0) then Result += ''; |
| 25 | +end; |
| 26 | + |
| 27 | + |
| 28 | +function T2DIntegerArray.ToBytes(): TByteArray; |
| 29 | +var |
| 30 | + size, i, len, offset: Integer; |
| 31 | +begin |
| 32 | + size := SizeOf(Int32); |
| 33 | + len := size; |
| 34 | + |
| 35 | + for i := 0 to High(Self) do |
| 36 | + Inc(len, size + Length(Self[i]) * size); |
| 37 | + |
| 38 | + SetLength(Result, len); |
| 39 | + Move(Length(Self), Result[0], size); |
| 40 | + offset := size; |
| 41 | + |
| 42 | + for i := 0 to High(Self) do |
| 43 | + begin |
| 44 | + len := Length(Self[i]); |
| 45 | + Move(len, Result[offset], size); |
| 46 | + Inc(offset, size); |
| 47 | + |
| 48 | + if len > 0 then |
| 49 | + begin |
| 50 | + Move(Self[i][0], Result[offset], len * size); |
| 51 | + Inc(offset, len * size); |
| 52 | + end; |
| 53 | + end; |
| 54 | +end; |
| 55 | + |
| 56 | +function T2DIntegerArray.CreateFromBytes(bytes: TByteArray): T2DIntegerArray; static; |
| 57 | +var |
| 58 | + size, i, len, row, offset: Integer; |
| 59 | +begin |
| 60 | + len := Length(bytes); |
| 61 | + if len = 0 then Exit; |
| 62 | + size := SizeOf(Integer); |
| 63 | + Move(bytes[0], len, size); |
| 64 | + SetLength(Result, len); |
| 65 | + offset := size; |
| 66 | + |
| 67 | + for i := 0 to High(Result) do |
| 68 | + begin |
| 69 | + Move(bytes[offset], row, size); |
| 70 | + SetLength(Result[i], row); |
| 71 | + Inc(offset, size); |
| 72 | + |
| 73 | + if row > 0 then |
| 74 | + begin |
| 75 | + Move(bytes[offset], Result[i][0], row * size); |
| 76 | + Inc(offset, row * size); |
| 77 | + end; |
| 78 | + end; |
| 79 | +end; |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +function TPointArray.ToBytes(): TByteArray; |
| 84 | +var |
| 85 | + len: Integer; |
| 86 | +begin |
| 87 | + len := Length(Self) * SizeOf(TPoint); |
| 88 | + if len = 0 then Exit; |
| 89 | + SetLength(Result, len); |
| 90 | + Move(Self[0], Result[0], len); |
| 91 | +end; |
| 92 | + |
| 93 | +function TPointArray.CreateFromBytes(bytes: TByteArray): TPointArray; static; |
| 94 | +var |
| 95 | + len: Integer; |
| 96 | +begin |
| 97 | + len := Length(bytes); |
| 98 | + if len = 0 then Exit; |
| 99 | + SetLength(Result, len div SizeOf(TPoint)); |
| 100 | + Move(bytes[0], Result[0], len); |
| 101 | +end; |
| 102 | + |
| 103 | + |
| 104 | +function T2DPointArray.ToBytes(): TByteArray; |
| 105 | +var |
| 106 | + size, i, len, offset: Int32; |
| 107 | + bytes: TByteArray; |
| 108 | +begin |
| 109 | + size := SizeOf(TPoint); |
| 110 | + len := SizeOf(Int32); // Start with the length of the outer array |
| 111 | + |
| 112 | + // Calculate the total size required for the byte array |
| 113 | + for i := 0 to High(Self) do |
| 114 | + Inc(len, SizeOf(Int32) + Length(Self[i]) * size); |
| 115 | + |
| 116 | + // Allocate the byte array |
| 117 | + SetLength(bytes, len); |
| 118 | + |
| 119 | + // Write the length of the outer array |
| 120 | + Move(Length(Self), bytes[0], SizeOf(Int32)); |
| 121 | + offset := SizeOf(Int32); |
| 122 | + |
| 123 | + // Write each inner array |
| 124 | + for i := 0 to High(Self) do |
| 125 | + begin |
| 126 | + len := Length(Self[i]); |
| 127 | + Move(len, bytes[offset], SizeOf(Int32)); // Write the length of the inner array |
| 128 | + Inc(offset, SizeOf(Int32)); |
| 129 | + |
| 130 | + if len > 0 then |
| 131 | + begin |
| 132 | + Move(Self[i][0], bytes[offset], len * size); // Write the inner array data |
| 133 | + Inc(offset, len * size); |
| 134 | + end; |
| 135 | + end; |
| 136 | + |
| 137 | + Result := bytes; |
| 138 | +end; |
| 139 | + |
| 140 | + |
| 141 | +function T2DPointArray.CreateFromBytes(bytes: TByteArray): T2DPointArray; static; |
| 142 | +var |
| 143 | + size, i, len, row, offset: Int32; |
| 144 | +begin |
| 145 | + size := SizeOf(TPoint); |
| 146 | + |
| 147 | + // Read the length of the outer array |
| 148 | + Move(bytes[0], len, SizeOf(Int32)); |
| 149 | + SetLength(Result, len); |
| 150 | + offset := SizeOf(Int32); |
| 151 | + |
| 152 | + // Read each inner array |
| 153 | + for i := 0 to High(Result) do |
| 154 | + begin |
| 155 | + Move(bytes[offset], row, SizeOf(Int32)); // Read the length of the inner array |
| 156 | + SetLength(Result[i], row); |
| 157 | + Inc(offset, SizeOf(Int32)); |
| 158 | + |
| 159 | + if row > 0 then |
| 160 | + begin |
| 161 | + Move(bytes[offset], Result[i][0], row * size); // Read the inner array data |
| 162 | + Inc(offset, row * size); |
| 163 | + end; |
| 164 | + end; |
| 165 | +end; |
0 commit comments