<commit_before>Red/System [
	Title:   "Red/System OS-independent runtime"
	Author:  "<NAME>"
	File: 	 %common.reds
	Rights:  "Copyright (C) 2011 <NAME>. All rights reserved."
	License: "BSD-3 - https://github.com/dockimbel/Red/blob/master/BSD-3-License.txt"
]

#define zero? 		  [0 =]
#define positive?	  [0 < ]				;-- space required after the lesser-than symbol
#define negative?	  [0 > ]
#define negate		  [0 -]
 
#define forever		  [while [true]]
#define does		  [func []]
#define unless		  [if not]
 
#define as-byte		  [as byte!]
#define as-logic	  [as logic!]
#define as-integer	  [as integer!]
#define as-c-string	  [as c-string!]
 
#define null-byte	  #"^(00)"
#define yes			  true
#define no			  false
#define on			  true
#define off			  false

#define byte-ptr!	  [pointer! [byte!]]
#define make-c-string [as c-string! allocate]


newline: 	"^/"
stdout:		-1								;-- uninitialized default value
stdin:		-1								;-- uninitialized default value
stderr:		-1								;-- uninitialized default value


system: struct [							;-- store runtime accessible system values
	reserved 	[integer!]					;-- place-holder to not have an empty structure
]


#switch OS [								;-- loading OS-specific bindings
	Windows  [#include %win32.reds]
	Syllable [#include %syllable.reds]
	#default [#include %linux.reds]
]

#either use-natives? = no [					;-- C bindings that have native counterparts
	#import [
		LIBC-file cdecl [
			allocate: "malloc" [
				size		[integer!]
				return:		[byte-ptr!]
			]
			free: "free" [
				block		[byte-ptr!]
			]
			set-memory:  "memset" [
				target		[byte-ptr!]
				filler		[byte!]
				size		[integer!]
				return:		[byte-ptr!]
			]
			copy-memory: "memmove" [
				target		[byte-ptr!]
				source		[byte-ptr!]
				size		[integer!]
				return:		[byte-ptr!]
			]
			length?: 	 "strlen" [
				command		[c-string!]
				return:		[integer!]
			]
		]
	]
][
	allocate:  func [						;-- needs a Red/System native implementation
		size		[integer!]
		return:		[byte-ptr!]
	][
		;TBD
	]
	
	free: func [							;-- needs a Red/System native implementation
		buffer		[byte-ptr!]
	][
		;TBD
	]
	
	set-memory: func [						;; fill a memory buffer with the same given byte ;;
		target		[byte-ptr!]				;; pointer to start of the buffer ;;
		filler		[byte!]					;; filler byte ;;
		size		[integer!]				;; size of the buffer to fill ;;
		return:		[byte-ptr!]				;; returns the start of the buffer ;;
	][
		unless zero? size [
			until [
				target/1: filler
				target: target + 1
				size: size - 1
				zero? size
			]
		]
		target
	]
	
	copy-memory: func [						;; copy a memory buffer to a new region in a safe way ;; 
		target		[byte-ptr!]				;; target start address ;;
		source		[byte-ptr!]				;; source start address ;;
		size		[integer!]				;; number of bytes to copy ;;
		return:		[byte-ptr!]				;; returns the target start address ;;
		/local c
	][
		unless any [zero? size source = target][
			either source < target [
				until [
					target/size: source/size
					size: size - 1
					zero? size
				]
			][
				c: 0
				until [
					target/c: source/c
					c: c + 1
					size: size - 1
					zero? size
				]
			]
		]
		target
	]

	length?: func [							;; return the number of characters from a c-string value ;;
		s 		[c-string!]					;; c-string value ;;
		return: [integer!]
		/local base
	][
		base: s
		while [s/1 <> null-byte][s: s + 1]
		as-integer s - base 				;-- do not count the terminal zero
	]
]


;-- Debugging helper functions --

prin-int: func [i [integer!] /local s c n][
	;-- dumbed down version of form-signed by <NAME> (https://gist.github.com/952998)
	;-- used in signal handlers, so dynamic allocation removed to limit interferences
	
	if zero? i [prin "0" exit]
	s: "           "						;-- 11 bytes wide
	n: negative? i
	if n [i: negate i]
	c: 11
	while [i <> 0][
		s/c: #"0" + (i // 10)
		i: i / 10
		c: c - 1
	]
	if n [s/c: #"-" c: c - 1]
	prin s + c
]
<commit_msg>FIX: added a special case for -2147483648 in PRIN-INT<commit_after>Red/System [
	Title:   "Red/System OS-independent runtime"
	Author:  "<NAME>"
	File: 	 %common.reds
	Rights:  "Copyright (C) 2011 <NAME>. All rights reserved."
	License: "BSD-3 - https://github.com/dockimbel/Red/blob/master/BSD-3-License.txt"
]

#define zero? 		  [0 =]
#define positive?	  [0 < ]				;-- space required after the lesser-than symbol
#define negative?	  [0 > ]
#define negate		  [0 -]
 
#define forever		  [while [true]]
#define does		  [func []]
#define unless		  [if not]
 
#define as-byte		  [as byte!]
#define as-logic	  [as logic!]
#define as-integer	  [as integer!]
#define as-c-string	  [as c-string!]
 
#define null-byte	  #"^(00)"
#define yes			  true
#define no			  false
#define on			  true
#define off			  false

#define byte-ptr!	  [pointer! [byte!]]
#define make-c-string [as c-string! allocate]


newline: 	"^/"
stdout:		-1								;-- uninitialized default value
stdin:		-1								;-- uninitialized default value
stderr:		-1								;-- uninitialized default value


system: struct [							;-- store runtime accessible system values
	reserved 	[integer!]					;-- place-holder to not have an empty structure
]


#switch OS [								;-- loading OS-specific bindings
	Windows  [#include %win32.reds]
	Syllable [#include %syllable.reds]
	#default [#include %linux.reds]
]

#either use-natives? = no [					;-- C bindings that have native counterparts
	#import [
		LIBC-file cdecl [
			allocate: "malloc" [
				size		[integer!]
				return:		[byte-ptr!]
			]
			free: "free" [
				block		[byte-ptr!]
			]
			set-memory:  "memset" [
				target		[byte-ptr!]
				filler		[byte!]
				size		[integer!]
				return:		[byte-ptr!]
			]
			copy-memory: "memmove" [
				target		[byte-ptr!]
				source		[byte-ptr!]
				size		[integer!]
				return:		[byte-ptr!]
			]
			length?: 	 "strlen" [
				command		[c-string!]
				return:		[integer!]
			]
		]
	]
][
	allocate:  func [						;-- needs a Red/System native implementation
		size		[integer!]
		return:		[byte-ptr!]
	][
		;TBD
	]
	
	free: func [							;-- needs a Red/System native implementation
		buffer		[byte-ptr!]
	][
		;TBD
	]
	
	set-memory: func [						;; fill a memory buffer with the same given byte ;;
		target		[byte-ptr!]				;; pointer to start of the buffer ;;
		filler		[byte!]					;; filler byte ;;
		size		[integer!]				;; size of the buffer to fill ;;
		return:		[byte-ptr!]				;; returns the start of the buffer ;;
	][
		unless zero? size [
			until [
				target/1: filler
				target: target + 1
				size: size - 1
				zero? size
			]
		]
		target
	]
	
	copy-memory: func [						;; copy a memory buffer to a new region in a safe way ;; 
		target		[byte-ptr!]				;; target start address ;;
		source		[byte-ptr!]				;; source start address ;;
		size		[integer!]				;; number of bytes to copy ;;
		return:		[byte-ptr!]				;; returns the target start address ;;
		/local c
	][
		unless any [zero? size source = target][
			either source < target [
				until [
					target/size: source/size
					size: size - 1
					zero? size
				]
			][
				c: 0
				until [
					target/c: source/c
					c: c + 1
					size: size - 1
					zero? size
				]
			]
		]
		target
	]

	length?: func [							;; return the number of characters from a c-string value ;;
		s 		[c-string!]					;; c-string value ;;
		return: [integer!]
		/local base
	][
		base: s
		while [s/1 <> null-byte][s: s + 1]
		as-integer s - base 				;-- do not count the terminal zero
	]
]


;-- Debugging helper functions --

prin-int: func [i [integer!] /local s c n][
	;-- dumbed down version of form-signed by <NAME> (https://gist.github.com/952998)
	;-- used in signal handlers, so dynamic allocation removed to limit interferences
	
	if zero? i [prin "0" exit]
	s: "-2147483648"						;-- 11 bytes wide
	if i = -2147483648 [prin s exit]
	n: negative? i
	if n [i: negate i]
	c: 11
	while [i <> 0][
		s/c: #"0" + (i // 10)
		i: i / 10
		c: c - 1
	]
	if n [s/c: #"-" c: c - 1]
	prin s + c
]

{'fraction_non_alphanumeric': 0.14372623574144486, 'fraction_numerical': 0.015209125475285171, 'mean_word_length': 2.2906588824020018, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5192112', 'n_tokens_mistral': 3658, 'n_tokens_neox': 2984, 'n_words': 889}