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 @@ -333,19 +333,27 @@ public function setBinaryBitsetLe($bitset)
333
333
if (!is_string ($ bitset )) {
334
334
throw new InvalidArgumentException ('Bitset must be a string ' );
335
335
}
336
-
337
- $ size = ceil ($ this ->ordinalMax / 8 );
336
+
337
+ $ size = strlen ($ this ->bitset );
338
338
$ sizeIn = strlen ($ bitset );
339
-
339
+
340
340
if ($ sizeIn < $ size ) {
341
341
// add "\0" if the given bitset is not long enough
342
342
$ bitset .= str_repeat ("\0" , $ size - $ sizeIn );
343
343
} elseif ($ sizeIn > $ size ) {
344
344
$ bitset = substr ($ bitset , 0 , $ size );
345
345
}
346
-
347
- $ this ->bitset = $ bitset ;
348
-
346
+
347
+ // truncate out-of-range bits of last byte
348
+ $ lastByteMaxOrd = $ this ->ordinalMax % 8 ;
349
+ if ($ lastByteMaxOrd === 0 ) {
350
+ $ this ->bitset = $ bitset ;
351
+ } else {
352
+ $ lastByte = chr ($ lastByteMaxOrd ) & $ bitset [$ size - 1 ];
353
+ $ this ->bitset = substr ($ bitset , 0 , -1 ) . $ lastByte ;
354
+ }
355
+
356
+ // reset the iterator position
349
357
$ this ->rewind ();
350
358
}
351
359
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