File tree Expand file tree Collapse file tree 2 files changed +28
-6
lines changed Expand file tree Collapse file tree 2 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -328,19 +328,27 @@ public function setBinaryBitsetLe($bitset)
328
328
if (!is_string ($ bitset )) {
329
329
throw new InvalidArgumentException ('Bitset must be a string ' );
330
330
}
331
-
332
- $ size = ceil ($ this ->ordinalMax / 8 );
331
+
332
+ $ size = strlen ($ this ->bitset );
333
333
$ sizeIn = strlen ($ bitset );
334
-
334
+
335
335
if ($ sizeIn < $ size ) {
336
336
// add "\0" if the given bitset is not long enough
337
337
$ bitset .= str_repeat ("\0" , $ size - $ sizeIn );
338
338
} elseif ($ sizeIn > $ size ) {
339
339
$ bitset = substr ($ bitset , 0 , $ size );
340
340
}
341
-
342
- $ this ->bitset = $ bitset ;
343
-
341
+
342
+ // truncate out-of-range bits of last byte
343
+ $ lastByteMaxOrd = $ this ->ordinalMax % 8 ;
344
+ if ($ lastByteMaxOrd === 0 ) {
345
+ $ this ->bitset = $ bitset ;
346
+ } else {
347
+ $ lastByte = chr ($ lastByteMaxOrd ) & $ bitset [$ size - 1 ];
348
+ $ this ->bitset = substr ($ bitset , 0 , -1 ) . $ lastByte ;
349
+ }
350
+
351
+ // reset the iterator position
344
352
$ this ->rewind ();
345
353
}
346
354
Original file line number Diff line number Diff line change @@ -368,6 +368,20 @@ public function testSetBinaryBitsetLe()
368
368
$ this ->assertTrue ($ enumSet ->count () == 3 );
369
369
}
370
370
371
+ public function testSetBinaryBitsetLeTruncateHighBits ()
372
+ {
373
+ $ enumSet = new EnumSet ('MabeEnumTest\TestAsset\Enum65 ' );
374
+ foreach (Enum65::getEnumerators () as $ enumerator ) {
375
+ $ enumSet ->attach ($ enumerator );
376
+ }
377
+
378
+ $ bitset = $ enumSet ->getBinaryBitsetLe ();
379
+ $ newBitset = substr ($ bitset , 0 , -1 ) . "\xff\xff" ;
380
+ $ enumSet ->setBinaryBitsetLe ($ newBitset );
381
+
382
+ $ this ->assertSame (bin2hex ($ bitset ), bin2hex ($ enumSet ->getBinaryBitsetLe ()));
383
+ }
384
+
371
385
public function testSetBinaryBitsetBe ()
372
386
{
373
387
$ enumSet = new EnumSet ('MabeEnumTest\TestAsset\Enum65 ' );
You can’t perform that action at this time.
0 commit comments