Copilot commented on code in PR #3144: URL: https://github.com/apache/fory/pull/3144#discussion_r2690081718
########## dart/packages/fory/lib/src/datatype/uint8.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt8: 8-bit unsigned integer (0 to 255) +final class UInt8 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 255; + + static UInt8 get maxValue => UInt8(MAX_VALUE); + static UInt8 get minValue => UInt8(MIN_VALUE); + + final int _value; + + UInt8(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 8-bit unsigned integer overflow behavior + return value & 0xFF; // Keep only the lowest 8 bits (0-255) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt8 operator +(dynamic other) => + UInt8(_value + (other is FixedNum ? other.value : other)); + + UInt8 operator -(dynamic other) => + UInt8(_value - (other is FixedNum ? other.value : other)); + + UInt8 operator *(dynamic other) => + UInt8(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt8 operator ~/(dynamic other) => + UInt8(_value ~/ (other is FixedNum ? other.value : other)); + + UInt8 operator %(dynamic other) => + UInt8(_value % (other is FixedNum ? other.value : other)); + + UInt8 operator -() => UInt8(-_value); + + // Bitwise operations + UInt8 operator &(dynamic other) => + UInt8(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator |(dynamic other) => + UInt8(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator ^(dynamic other) => + UInt8(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator ~() => UInt8(~_value); + + UInt8 operator <<(int shiftAmount) => UInt8(_value << shiftAmount); + UInt8 operator >>(int shiftAmount) => UInt8(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); + int get sign => _value.sign; + bool get isNegative => _value < 0; Review Comment: For an unsigned integer type, `isNegative` should always return `false` since the valid range is 0-255. The current implementation incorrectly checks if `_value < 0`, which should never be true after the wrapping logic in `_convert`. This property should be `bool get isNegative => false;` to reflect the unsigned nature of the type. ```suggestion bool get isNegative => false; ``` ########## dart/packages/fory/lib/src/datatype/uint16.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt16: 16-bit unsigned integer (0 to 65535) +final class UInt16 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 65535; + + static UInt16 get maxValue => UInt16(MAX_VALUE); + static UInt16 get minValue => UInt16(MIN_VALUE); + + final int _value; + + UInt16(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 16-bit unsigned integer overflow behavior + return value & 0xFFFF; // Keep only the lowest 16 bits (0-65535) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt16 operator +(dynamic other) => + UInt16(_value + (other is FixedNum ? other.value : other)); + + UInt16 operator -(dynamic other) => + UInt16(_value - (other is FixedNum ? other.value : other)); + + UInt16 operator *(dynamic other) => + UInt16(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt16 operator ~/(dynamic other) => + UInt16(_value ~/ (other is FixedNum ? other.value : other)); + + UInt16 operator %(dynamic other) => + UInt16(_value % (other is FixedNum ? other.value : other)); + + UInt16 operator -() => UInt16(-_value); + + // Bitwise operations + UInt16 operator &(dynamic other) => + UInt16(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator |(dynamic other) => + UInt16(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator ^(dynamic other) => + UInt16(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator ~() => UInt16(~_value); + + UInt16 operator <<(int shiftAmount) => UInt16(_value << shiftAmount); + UInt16 operator >>(int shiftAmount) => UInt16(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); + int get sign => _value.sign; + bool get isNegative => _value < 0; Review Comment: For an unsigned integer type, `isNegative` should always return `false` since the valid range is 0-65535. The current implementation incorrectly checks if `_value < 0`, which should never be true after the wrapping logic in `_convert`. This property should be `bool get isNegative => false;` to reflect the unsigned nature of the type. ```suggestion bool get isNegative => false; ``` ########## dart/packages/fory/lib/src/datatype/uint16.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt16: 16-bit unsigned integer (0 to 65535) +final class UInt16 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 65535; + + static UInt16 get maxValue => UInt16(MAX_VALUE); + static UInt16 get minValue => UInt16(MIN_VALUE); + + final int _value; + + UInt16(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 16-bit unsigned integer overflow behavior + return value & 0xFFFF; // Keep only the lowest 16 bits (0-65535) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt16 operator +(dynamic other) => + UInt16(_value + (other is FixedNum ? other.value : other)); + + UInt16 operator -(dynamic other) => + UInt16(_value - (other is FixedNum ? other.value : other)); + + UInt16 operator *(dynamic other) => + UInt16(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt16 operator ~/(dynamic other) => + UInt16(_value ~/ (other is FixedNum ? other.value : other)); + + UInt16 operator %(dynamic other) => + UInt16(_value % (other is FixedNum ? other.value : other)); + + UInt16 operator -() => UInt16(-_value); + + // Bitwise operations + UInt16 operator &(dynamic other) => + UInt16(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator |(dynamic other) => + UInt16(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator ^(dynamic other) => + UInt16(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator ~() => UInt16(~_value); + + UInt16 operator <<(int shiftAmount) => UInt16(_value << shiftAmount); + UInt16 operator >>(int shiftAmount) => UInt16(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); Review Comment: For an unsigned integer type, `abs()` should simply return `_value` since unsigned values are always non-negative. Calling `.abs()` on an already non-negative value is unnecessary and may cause confusion about the semantics of this type. ```suggestion int abs() => _value; ``` ########## dart/packages/fory/lib/src/datatype/uint8.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt8: 8-bit unsigned integer (0 to 255) +final class UInt8 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 255; + + static UInt8 get maxValue => UInt8(MAX_VALUE); + static UInt8 get minValue => UInt8(MIN_VALUE); + + final int _value; + + UInt8(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 8-bit unsigned integer overflow behavior + return value & 0xFF; // Keep only the lowest 8 bits (0-255) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt8 operator +(dynamic other) => + UInt8(_value + (other is FixedNum ? other.value : other)); + + UInt8 operator -(dynamic other) => + UInt8(_value - (other is FixedNum ? other.value : other)); + + UInt8 operator *(dynamic other) => + UInt8(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt8 operator ~/(dynamic other) => + UInt8(_value ~/ (other is FixedNum ? other.value : other)); + + UInt8 operator %(dynamic other) => + UInt8(_value % (other is FixedNum ? other.value : other)); + + UInt8 operator -() => UInt8(-_value); + + // Bitwise operations + UInt8 operator &(dynamic other) => + UInt8(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator |(dynamic other) => + UInt8(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator ^(dynamic other) => + UInt8(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator ~() => UInt8(~_value); + + UInt8 operator <<(int shiftAmount) => UInt8(_value << shiftAmount); + UInt8 operator >>(int shiftAmount) => UInt8(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); + int get sign => _value.sign; Review Comment: For an unsigned integer type, the `sign` property should return `0` when `_value == 0` and `1` otherwise, rather than delegating to `_value.sign`. Since unsigned integers are never negative, the sign is always 0 or 1. ```suggestion int get sign => _value == 0 ? 0 : 1; ``` ########## dart/packages/fory/lib/src/datatype/uint8.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt8: 8-bit unsigned integer (0 to 255) +final class UInt8 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 255; + + static UInt8 get maxValue => UInt8(MAX_VALUE); + static UInt8 get minValue => UInt8(MIN_VALUE); + + final int _value; + + UInt8(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 8-bit unsigned integer overflow behavior + return value & 0xFF; // Keep only the lowest 8 bits (0-255) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt8 operator +(dynamic other) => + UInt8(_value + (other is FixedNum ? other.value : other)); + + UInt8 operator -(dynamic other) => + UInt8(_value - (other is FixedNum ? other.value : other)); + + UInt8 operator *(dynamic other) => + UInt8(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt8 operator ~/(dynamic other) => + UInt8(_value ~/ (other is FixedNum ? other.value : other)); + + UInt8 operator %(dynamic other) => + UInt8(_value % (other is FixedNum ? other.value : other)); + + UInt8 operator -() => UInt8(-_value); + + // Bitwise operations + UInt8 operator &(dynamic other) => + UInt8(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator |(dynamic other) => + UInt8(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator ^(dynamic other) => + UInt8(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt8 operator ~() => UInt8(~_value); + + UInt8 operator <<(int shiftAmount) => UInt8(_value << shiftAmount); + UInt8 operator >>(int shiftAmount) => UInt8(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); Review Comment: For an unsigned integer type, `abs()` should simply return `_value` since unsigned values are always non-negative. Calling `.abs()` on an already non-negative value is unnecessary and may cause confusion about the semantics of this type. ```suggestion int abs() => _value; ``` ########## dart/packages/fory/lib/src/datatype/uint32.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt32: 32-bit unsigned integer (0 to 4294967295) +final class UInt32 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 4294967295; + + static UInt32 get maxValue => UInt32(MAX_VALUE); + static UInt32 get minValue => UInt32(MIN_VALUE); + + final int _value; + + UInt32(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 32-bit unsigned integer overflow behavior + return value & 0xFFFFFFFF; // Keep only the lowest 32 bits (0-4294967295) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt32 operator +(dynamic other) => + UInt32(_value + (other is FixedNum ? other.value : other)); + + UInt32 operator -(dynamic other) => + UInt32(_value - (other is FixedNum ? other.value : other)); + + UInt32 operator *(dynamic other) => + UInt32(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt32 operator ~/(dynamic other) => + UInt32(_value ~/ (other is FixedNum ? other.value : other)); + + UInt32 operator %(dynamic other) => + UInt32(_value % (other is FixedNum ? other.value : other)); + + UInt32 operator -() => UInt32(-_value); + + // Bitwise operations + UInt32 operator &(dynamic other) => + UInt32(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator |(dynamic other) => + UInt32(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator ^(dynamic other) => + UInt32(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator ~() => UInt32(~_value); + + UInt32 operator <<(int shiftAmount) => UInt32(_value << shiftAmount); + UInt32 operator >>(int shiftAmount) => UInt32(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); + int get sign => _value.sign; Review Comment: For an unsigned integer type, the `sign` property should return `0` when `_value == 0` and `1` otherwise, rather than delegating to `_value.sign`. Since unsigned integers are never negative, the sign is always 0 or 1. ```suggestion int get sign => _value == 0 ? 0 : 1; ``` ########## dart/packages/fory/lib/src/datatype/uint32.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt32: 32-bit unsigned integer (0 to 4294967295) +final class UInt32 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 4294967295; + + static UInt32 get maxValue => UInt32(MAX_VALUE); + static UInt32 get minValue => UInt32(MIN_VALUE); + + final int _value; + + UInt32(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 32-bit unsigned integer overflow behavior + return value & 0xFFFFFFFF; // Keep only the lowest 32 bits (0-4294967295) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt32 operator +(dynamic other) => + UInt32(_value + (other is FixedNum ? other.value : other)); + + UInt32 operator -(dynamic other) => + UInt32(_value - (other is FixedNum ? other.value : other)); + + UInt32 operator *(dynamic other) => + UInt32(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt32 operator ~/(dynamic other) => + UInt32(_value ~/ (other is FixedNum ? other.value : other)); + + UInt32 operator %(dynamic other) => + UInt32(_value % (other is FixedNum ? other.value : other)); + + UInt32 operator -() => UInt32(-_value); + + // Bitwise operations + UInt32 operator &(dynamic other) => + UInt32(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator |(dynamic other) => + UInt32(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator ^(dynamic other) => + UInt32(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator ~() => UInt32(~_value); + + UInt32 operator <<(int shiftAmount) => UInt32(_value << shiftAmount); + UInt32 operator >>(int shiftAmount) => UInt32(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); Review Comment: For an unsigned integer type, `abs()` should simply return `_value` since unsigned values are always non-negative. Calling `.abs()` on an already non-negative value is unnecessary and may cause confusion about the semantics of this type. ```suggestion int abs() => _value; ``` ########## dart/packages/fory/lib/src/datatype/uint16.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt16: 16-bit unsigned integer (0 to 65535) +final class UInt16 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 65535; + + static UInt16 get maxValue => UInt16(MAX_VALUE); + static UInt16 get minValue => UInt16(MIN_VALUE); + + final int _value; + + UInt16(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 16-bit unsigned integer overflow behavior + return value & 0xFFFF; // Keep only the lowest 16 bits (0-65535) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt16 operator +(dynamic other) => + UInt16(_value + (other is FixedNum ? other.value : other)); + + UInt16 operator -(dynamic other) => + UInt16(_value - (other is FixedNum ? other.value : other)); + + UInt16 operator *(dynamic other) => + UInt16(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt16 operator ~/(dynamic other) => + UInt16(_value ~/ (other is FixedNum ? other.value : other)); + + UInt16 operator %(dynamic other) => + UInt16(_value % (other is FixedNum ? other.value : other)); + + UInt16 operator -() => UInt16(-_value); + + // Bitwise operations + UInt16 operator &(dynamic other) => + UInt16(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator |(dynamic other) => + UInt16(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator ^(dynamic other) => + UInt16(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt16 operator ~() => UInt16(~_value); + + UInt16 operator <<(int shiftAmount) => UInt16(_value << shiftAmount); + UInt16 operator >>(int shiftAmount) => UInt16(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); + int get sign => _value.sign; Review Comment: For an unsigned integer type, the `sign` property should return `0` when `_value == 0` and `1` otherwise, rather than delegating to `_value.sign`. Since unsigned integers are never negative, the sign is always 0 or 1. ```suggestion int get sign => _value == 0 ? 0 : 1; ``` ########## dart/packages/fory/lib/src/datatype/uint32.dart: ########## @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import 'fory_fixed_num.dart'; + +/// UInt32: 32-bit unsigned integer (0 to 4294967295) +final class UInt32 extends FixedNum { + static const int MIN_VALUE = 0; + static const int MAX_VALUE = 4294967295; + + static UInt32 get maxValue => UInt32(MAX_VALUE); + static UInt32 get minValue => UInt32(MIN_VALUE); + + final int _value; + + UInt32(num input) : _value = _convert(input); + + static int _convert(num value) { + if (value is int) { + // Apply 32-bit unsigned integer overflow behavior + return value & 0xFFFFFFFF; // Keep only the lowest 32 bits (0-4294967295) + } else { + return _convert(value.toInt()); + } + } + + @override + int get value => _value; + + // Operators + UInt32 operator +(dynamic other) => + UInt32(_value + (other is FixedNum ? other.value : other)); + + UInt32 operator -(dynamic other) => + UInt32(_value - (other is FixedNum ? other.value : other)); + + UInt32 operator *(dynamic other) => + UInt32(_value * (other is FixedNum ? other.value : other)); + + double operator /(dynamic other) => + _value / (other is FixedNum ? other.value : other); + + UInt32 operator ~/(dynamic other) => + UInt32(_value ~/ (other is FixedNum ? other.value : other)); + + UInt32 operator %(dynamic other) => + UInt32(_value % (other is FixedNum ? other.value : other)); + + UInt32 operator -() => UInt32(-_value); + + // Bitwise operations + UInt32 operator &(dynamic other) => + UInt32(_value & (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator |(dynamic other) => + UInt32(_value | (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator ^(dynamic other) => + UInt32(_value ^ (other is FixedNum ? other.value : other).toInt()); + + UInt32 operator ~() => UInt32(~_value); + + UInt32 operator <<(int shiftAmount) => UInt32(_value << shiftAmount); + UInt32 operator >>(int shiftAmount) => UInt32(_value >> shiftAmount); + + // Comparison + bool operator <(dynamic other) => + _value < (other is FixedNum ? other.value : other); + + bool operator <=(dynamic other) => + _value <= (other is FixedNum ? other.value : other); + + bool operator >(dynamic other) => + _value > (other is FixedNum ? other.value : other); + + bool operator >=(dynamic other) => + _value >= (other is FixedNum ? other.value : other); + + // Equality + @override + bool operator ==(Object other) { + if (other is FixedNum) return _value == other.value; + if (other is num) return _value == other; + return false; + } + + @override + int get hashCode => _value.hashCode; + + // Common num methods + int abs() => _value.abs(); + int get sign => _value.sign; + bool get isNegative => _value < 0; Review Comment: For an unsigned integer type, `isNegative` should always return `false` since the valid range is 0-4294967295. The current implementation incorrectly checks if `_value < 0`, which should never be true after the wrapping logic in `_convert`. This property should be `bool get isNegative => false;` to reflect the unsigned nature of the type. ```suggestion bool get isNegative => false; ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
