
// Generic header
#include "scolPrerequisites.h"

int BLG_memcpy8(void *dst, void *src, int len)
{
	#if SCOL_PLATFORM == SCOL_PLATFORM_WINDOWS
		_asm
		{
			//Saving registries
			push eax
			push ebx
			push ecx
			push edx
			//Storing data
			mov eax, dst
			mov ebx, src
			mov ecx, len
			//Checking empty string
			cmp ecx, 0
			//$BLG - v5.22: Modif
			//je  LOOPEND
			jle LOOPEND
			//Copying data
			//Tried to optimize next lines, with blocks of 4,2 & 1 byte(s) depending on ecx value : no noticeable performance improvement		
			LOOPSTART:
			mov dl, [ebx]
			mov [eax], dl
			inc eax
			inc ebx
			dec ecx
			cmp ecx, 0
			jne LOOPSTART
			LOOPEND:
			//Restoring registries
			pop edx
			pop ecx
			pop ebx
			pop eax
		}
	#else
		// For linux version, use the standard memcpy function
		memcpy(dst, src, len);
	#endif
	return 0;
}


int BLG_lowercase(char* src, int len)
{
	#if SCOL_PLATFORM == SCOL_PLATFORM_WINDOWS
		_asm
		{
			//Saving registries
			push eax
			push ebx
			push ecx
			//Storing data
			mov eax, src
			mov ecx, len
			//Checking empty string
			cmp ecx, 0
			//$BLG - v5.22: Modif
			//je  LOOPEND
			jle LOOPEND
			//Checking and modifying case
			LOOPSTART:
			mov bl, [eax]
			cmp bl, 65
			jl LOOPAGAIN			// ignore char if < 'A'
			cmp bl, 90
			jg LOOPAGAIN			// ignore char if > 'Z'
			add bl, 32
			mov [eax], bl		// add 'a'-'A' if uppercase to get lowercase
			LOOPAGAIN:
			inc eax
			dec ecx
			cmp ecx, 0
			jne LOOPSTART
			LOOPEND:
			//Restoring registries
			pop ecx
			pop ebx
			pop eax
		}
	#else
		// Linux version
		char diff = 'a'-'A';
		for(int k=0; k<len; k++)
			if ((src[k]>='A')&&(src[k]<='Z'))
				src[k]+=diff;
	#endif
	return 0;
}


int BLG_uppercase(char* src, int len)
{
	#if SCOL_PLATFORM == SCOL_PLATFORM_WINDOWS
		_asm
		{
			//Saving registries
			push eax
			push ebx
			push ecx
			//Storing data
			mov eax, src
			mov ecx, len
			//Checking empty string
			cmp ecx, 0
			//$BLG - v5.22: Modif
			//je  LOOPEND
			jle  LOOPEND
			//Checking and modifying case
			LOOPSTART:
			mov bl, [eax]
			cmp bl, 97
			jl LOOPAGAIN			// ignore char if < 'a'
			cmp bl, 122
			jg LOOPAGAIN			// ignore char if > 'z'
			sub bl, 32
			mov [eax], bl		// sub 'a'-'A' if lowercase to get uppercase
			LOOPAGAIN:
			inc eax
			dec ecx
			cmp ecx, 0
			jne LOOPSTART
			LOOPEND:
			//Restoring registries
			pop ecx
			pop ebx
			pop eax
		}
	#else
		// Linux version
		char diff = 'a'-'A';
		for(int k=0; k<len; k++)
			if ((src[k]>='a') && (src[k]<='z'))
				src[k]-=diff;
	#endif
	return 0;
}

