UninterpretedBytes.st
changeset 14142 7a769e54130d
parent 14132 debafd18f04b
child 14155 065cc819c3f1
equal deleted inserted replaced
14141:e86d245213b0 14142:7a769e54130d
  2446 !UninterpretedBytes methodsFor:'converting'!
  2446 !UninterpretedBytes methodsFor:'converting'!
  2447 
  2447 
  2448 asExternalBytes
  2448 asExternalBytes
  2449     |sz bytes|
  2449     |sz bytes|
  2450 
  2450 
  2451     sz := self size.
  2451     sz := self byteSize.
  2452     bytes := ExternalBytes unprotectedNew:sz.
  2452     bytes := ExternalBytes unprotectedNew:sz.
  2453     bytes replaceBytesFrom:1 to:sz with:self startingAt:1.
  2453     bytes replaceBytesFrom:1 to:sz with:self startingAt:1.
  2454     ^ bytes
  2454     ^ bytes
  2455 
  2455 
  2456     "
  2456     "
  2457       #[1 2 3 4 5 6 7] asExternalBytes
  2457       #[1 2 3 4 5 6 7] asExternalBytes
  2458       'Hello World' asExternalBytes
  2458       'Hello World' asExternalBytes
       
  2459       'Hello World' asUnicodeString asExternalBytes
  2459     "
  2460     "
  2460 !
  2461 !
  2461 
  2462 
  2462 asUUID
  2463 asUUID
  2463     ^ UUID fromBytes:self
  2464     ^ UUID fromBytes:self
  2482     REGISTER unsigned char *dst;
  2483     REGISTER unsigned char *dst;
  2483     OBJ cls;
  2484     OBJ cls;
  2484 
  2485 
  2485 #ifndef NO_PRIM_BYTEARR
  2486 #ifndef NO_PRIM_BYTEARR
  2486     if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection))
  2487     if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection))
  2487      && __isBytes(self)
  2488      && (__isBytes(self) || __isWords(self))
  2488      && __bothSmallInteger(start, stop)
  2489      && __bothSmallInteger(start, stop)
  2489      && __isSmallInteger(repStart)) {
  2490      && __isSmallInteger(repStart)) {
  2490 	startIndex = __intVal(start) - 1;
  2491         startIndex = __intVal(start) - 1;
  2491 	if (startIndex >= 0) {
  2492         if (startIndex >= 0) {
  2492 	    dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex;
  2493             dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex;
  2493 	    nIndex = __byteArraySize(self);
  2494             nIndex = __byteArraySize(self);
  2494 
  2495 
  2495 	    if ((cls = __qClass(self)) != @global(ByteArray)) {
  2496             if ((cls = __qClass(self)) != @global(ByteArray)) {
  2496 		int nInst;
  2497                 int nInst;
  2497 
  2498 
  2498 		nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
  2499                 nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
  2499 		dst += nInst;
  2500                 dst += nInst;
  2500 		nIndex -= nInst;
  2501                 nIndex -= nInst;
  2501 	    }
  2502             }
  2502 
  2503 
  2503 	    stopIndex = __intVal(stop) - 1;
  2504             stopIndex = __intVal(stop) - 1;
  2504 	    count = stopIndex - startIndex + 1;
  2505             count = stopIndex - startIndex + 1;
  2505 	    if (count == 0) {
  2506             if (count == 0) {
  2506 		RETURN ( self );
  2507                 RETURN ( self );
  2507 	    }
  2508             }
  2508 
  2509 
  2509 	    if ((count > 0) && (stopIndex < nIndex)) {
  2510             if ((count > 0) && (stopIndex < nIndex)) {
  2510 		repStartIndex = __intVal(repStart) - 1;
  2511                 repStartIndex = __intVal(repStart) - 1;
  2511 		if (repStartIndex >= 0) {
  2512                 if (repStartIndex >= 0) {
  2512 		    if (__isExternalBytesLike(aCollection)) {
  2513                     if (__isExternalBytesLike(aCollection)) {
  2513 			OBJ sz;
  2514                         OBJ sz;
  2514 
  2515 
  2515 			src = __externalAddressVal(aCollection);
  2516                         src = __externalAddressVal(aCollection);
  2516 			if (src == 0) goto fallBack;
  2517                         if (src == 0) goto fallBack;
  2517 
  2518 
  2518 			sz = __externalBytesSize(aCollection);
  2519                         sz = __externalBytesSize(aCollection);
  2519 			if (__isSmallInteger(sz)) {
  2520                         if (__isSmallInteger(sz)) {
  2520 			    repNIndex = __smallIntegerVal(sz);
  2521                             repNIndex = __smallIntegerVal(sz);
  2521 			} else {
  2522                         } else {
  2522 			    repNIndex = repStopIndex+1; /* always enough */
  2523                             repNIndex = repStopIndex+1; /* always enough */
  2523 			}
  2524                         }
  2524 			src = src + repStartIndex;
  2525                         src = src + repStartIndex;
  2525 		    } else {
  2526                     } else {
  2526 			if (__isString(aCollection)) {
  2527                         if (__isString(aCollection)) {
  2527 			    repNIndex = __stringSize(aCollection);
  2528                             repNIndex = __stringSize(aCollection);
  2528 			} else {
  2529                         } else {
  2529 			    repNIndex = __qSize(aCollection) - OHDR_SIZE;
  2530                             repNIndex = __qSize(aCollection) - OHDR_SIZE;
  2530 			}
  2531                         }
  2531 			src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex;
  2532                         src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex;
  2532 			if ((cls = __qClass(aCollection)) != @global(ByteArray)) {
  2533                         if ((cls = __qClass(aCollection)) != @global(ByteArray)) {
  2533 			    int nInst;
  2534                             int nInst;
  2534 
  2535 
  2535 			    nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
  2536                             nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
  2536 			    src += nInst;
  2537                             src += nInst;
  2537 			    repNIndex -= nInst;
  2538                             repNIndex -= nInst;
  2538 			}
  2539                         }
  2539 		    }
  2540                     }
  2540 		    repStopIndex = repStartIndex + (stopIndex - startIndex);
  2541                     repStopIndex = repStartIndex + (stopIndex - startIndex);
  2541 		    if (repStopIndex < repNIndex) {
  2542                     if (repStopIndex < repNIndex) {
  2542 			if (aCollection == self) {
  2543                         if (aCollection == self) {
  2543 			    /* take care of overlapping copy */
  2544                             /* take care of overlapping copy */
  2544 			    if (src < dst) {
  2545                             if (src < dst) {
  2545 				/* must do a reverse copy */
  2546                                 /* must do a reverse copy */
  2546 				src += count;
  2547                                 src += count;
  2547 				dst += count;
  2548                                 dst += count;
  2548 				while (count-- > 0) {
  2549                                 while (count-- > 0) {
  2549 				    *--dst = *--src;
  2550                                     *--dst = *--src;
  2550 				}
  2551                                 }
  2551 				RETURN ( self );
  2552                                 RETURN ( self );
  2552 			    }
  2553                             }
  2553 			}
  2554                         }
  2554 
  2555 
  2555 # ifdef bcopy4
  2556 # ifdef bcopy4
  2556 			if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) {
  2557                         if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) {
  2557 			    int nW;
  2558                             int nW;
  2558 
  2559 
  2559 			    /* copy unaligned part */
  2560                             /* copy unaligned part */
  2560 			    while (count && ((unsigned INT)src & 3)) {
  2561                             while (count && ((unsigned INT)src & 3)) {
  2561 				*dst++ = *src++;
  2562                                 *dst++ = *src++;
  2562 				count--;
  2563                                 count--;
  2563 			    }
  2564                             }
  2564 
  2565 
  2565 			    if (count > 0) {
  2566                             if (count > 0) {
  2566 				/* copy aligned part */
  2567                                 /* copy aligned part */
  2567 				nW = count >> 2;
  2568                                 nW = count >> 2;
  2568 				bcopy4(src, dst, nW);
  2569                                 bcopy4(src, dst, nW);
  2569 				if ((count = count & 3) != 0) {
  2570                                 if ((count = count & 3) != 0) {
  2570 				    /* copy any remaining part */
  2571                                     /* copy any remaining part */
  2571 				    src += (nW<<2);
  2572                                     src += (nW<<2);
  2572 				    dst += (nW<<2);
  2573                                     dst += (nW<<2);
  2573 				    while (count--) {
  2574                                     while (count--) {
  2574 					*dst++ = *src++;
  2575                                         *dst++ = *src++;
  2575 				    }
  2576                                     }
  2576 				}
  2577                                 }
  2577 			    }
  2578                             }
  2578 			    RETURN ( self );
  2579                             RETURN ( self );
  2579 			}
  2580                         }
  2580 # else
  2581 # else
  2581 #  if __POINTER_SIZE__ == 8
  2582 #  if __POINTER_SIZE__ == 8
  2582 			if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) {
  2583                         if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) {
  2583 			    /* copy unaligned part */
  2584                             /* copy unaligned part */
  2584 			    while (count && ((unsigned INT)src & 7)) {
  2585                             while (count && ((unsigned INT)src & 7)) {
  2585 				*dst++ = *src++;
  2586                                 *dst++ = *src++;
  2586 				count--;
  2587                                 count--;
  2587 			    }
  2588                             }
  2588 
  2589 
  2589 			    /* copy aligned part */
  2590                             /* copy aligned part */
  2590 			    while (count >= 8) {
  2591                             while (count >= 8) {
  2591 				((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0];
  2592                                 ((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0];
  2592 				dst += 8;
  2593                                 dst += 8;
  2593 				src += 8;
  2594                                 src += 8;
  2594 				count -= 8;
  2595                                 count -= 8;
  2595 			    }
  2596                             }
  2596 			    while (count--) {
  2597                             while (count--) {
  2597 				*dst++ = *src++;
  2598                                 *dst++ = *src++;
  2598 			    }
  2599                             }
  2599 			    RETURN ( self );
  2600                             RETURN ( self );
  2600 			}
  2601                         }
  2601 #  endif /* 64bit */
  2602 #  endif /* 64bit */
  2602 # endif /* bcopy4 */
  2603 # endif /* bcopy4 */
  2603 
  2604 
  2604 # ifdef FAST_MEMCPY
  2605 # ifdef FAST_MEMCPY
  2605 			bcopy(src, dst, count);
  2606                         bcopy(src, dst, count);
  2606 # else
  2607 # else
  2607 #  ifdef __UNROLL_LOOPS__
  2608 #  ifdef __UNROLL_LOOPS__
  2608 			while (count >= 8) {
  2609                         while (count >= 8) {
  2609 			    dst[0] = src[0]; dst[1] = src[1];
  2610                             dst[0] = src[0]; dst[1] = src[1];
  2610 			    dst[2] = src[2]; dst[3] = src[3];
  2611                             dst[2] = src[2]; dst[3] = src[3];
  2611 			    dst[4] = src[4]; dst[5] = src[5];
  2612                             dst[4] = src[4]; dst[5] = src[5];
  2612 			    dst[6] = src[6]; dst[7] = src[7];
  2613                             dst[6] = src[6]; dst[7] = src[7];
  2613 			    dst += 8; src += 8;
  2614                             dst += 8; src += 8;
  2614 			    count -= 8;
  2615                             count -= 8;
  2615 			}
  2616                         }
  2616 #  endif /* __UNROLL_LOOPS__ */
  2617 #  endif /* __UNROLL_LOOPS__ */
  2617 			while (count-- > 0) {
  2618                         while (count-- > 0) {
  2618 			    *dst++ = *src++;
  2619                             *dst++ = *src++;
  2619 			}
  2620                         }
  2620 # endif
  2621 # endif
  2621 			RETURN ( self );
  2622                         RETURN ( self );
  2622 		    }
  2623                     }
  2623 		}
  2624                 }
  2624 	    }
  2625             }
  2625 	}
  2626         }
  2626     }
  2627     }
  2627 fallBack: ;
  2628 fallBack: ;
  2628 #endif
  2629 #endif
  2629 %}.
  2630 %}.
  2630     "
  2631     "
  2633     "
  2634     "
  2634     self slowReplaceBytesFrom:start to:stop with:aCollection startingAt:repStart
  2635     self slowReplaceBytesFrom:start to:stop with:aCollection startingAt:repStart
  2635 
  2636 
  2636     "
  2637     "
  2637      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2638      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2638 	copy
  2639         copy
  2639 	    replaceFrom:1 to:8
  2640             replaceFrom:1 to:8
  2640 	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2641             with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2641 	    startingAt:1
  2642             startingAt:1
  2642 
  2643 
  2643      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2644      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2644 	copy
  2645         copy
  2645 	    replaceFrom:3 to:10
  2646             replaceFrom:3 to:10
  2646 	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2647             with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2647 	    startingAt:1
  2648             startingAt:1
  2648 
  2649 
  2649      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2650      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2650 	copy
  2651         copy
  2651 	    replaceFrom:3 to:4
  2652             replaceFrom:3 to:4
  2652 	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2653             with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2653 	    startingAt:1
  2654             startingAt:1
  2654 
  2655 
  2655      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2656      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2656 	copy
  2657         copy
  2657 	    replaceFrom:0 to:9
  2658             replaceFrom:0 to:9
  2658 	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2659             with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2659 	    startingAt:1
  2660             startingAt:1
  2660 
  2661 
  2661      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2662      #[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  2662 	copy
  2663         copy
  2663 	    replaceFrom:1 to:10
  2664             replaceFrom:1 to:10
  2664 	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2665             with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
  2665 	    startingAt:0
  2666             startingAt:0
  2666     "
  2667     "
  2667 !
  2668 !
  2668 
  2669 
  2669 replaceBytesFrom:startIndex with:replacementCollection startingAt:repStartIndex
  2670 replaceBytesFrom:startIndex with:replacementCollection startingAt:repStartIndex
  2670     "replace elements from another collection, which must be
  2671     "replace elements from another collection, which must be
  2937 ! !
  2938 ! !
  2938 
  2939 
  2939 !UninterpretedBytes class methodsFor:'documentation'!
  2940 !UninterpretedBytes class methodsFor:'documentation'!
  2940 
  2941 
  2941 version
  2942 version
  2942     ^ '$Header: /cvs/stx/stx/libbasic/UninterpretedBytes.st,v 1.86 2012-05-13 19:29:47 stefan Exp $'
  2943     ^ '$Header: /cvs/stx/stx/libbasic/UninterpretedBytes.st,v 1.87 2012-05-16 22:28:00 stefan Exp $'
  2943 !
  2944 !
  2944 
  2945 
  2945 version_CVS
  2946 version_CVS
  2946     ^ '$Header: /cvs/stx/stx/libbasic/UninterpretedBytes.st,v 1.86 2012-05-13 19:29:47 stefan Exp $'
  2947     ^ '$Header: /cvs/stx/stx/libbasic/UninterpretedBytes.st,v 1.87 2012-05-16 22:28:00 stefan Exp $'
  2947 ! !
  2948 ! !