<commit_before>package org.lwjgl.generator

import java.io.PrintWriter

private trait FunctionTransform<T: QualifiedType> {
	fun transformDeclaration(param: T, original: String): String?
	fun transformCall(param: T, original: String): String
}

/** A function transform that must perform some pre-processing. */
private trait PreFunctionTransform<T: QualifiedType> {
	fun preprocess(qtype: T, writer: PrintWriter)
}

/** A function transform that makes use of the APIBuffer. */
private trait APIBufferFunctionTransform<T: QualifiedType> {
	fun setupAPIBuffer(func: Function, qtype: T, writer: PrintWriter)
}

/** Marker trait to indicate that buffer checks should be skipped. */
private trait SkipCheckFunctionTransform

private fun <T: QualifiedType> T.transformDeclarationOrElse(transforms: Map<QualifiedType, FunctionTransform<out QualifiedType>>, original: String): String? {
	val transform = transforms[this]
	if ( transform == null )
		return original
	else
		return [suppress("UNCHECKED_CAST")](transform as FunctionTransform<T>).transformDeclaration(this, original)
}

private fun <T: QualifiedType> T.transformCallOrElse(transforms: Map<QualifiedType, FunctionTransform<out QualifiedType>>, original: String): String {
	val transform = transforms[this]
	if ( transform == null )
		return original
	else
		return [suppress("UNCHECKED_CAST")](transform as FunctionTransform<T>).transformCall(this, original)
}

private open class AutoSizeTransform(val bufferParam: Parameter, val applyFactor: Boolean = true): FunctionTransform<Parameter> {
	override fun transformDeclaration(param: Parameter, original: String) = null // Remove the parameter
	override fun transformCall(param: Parameter, original: String): String {
		var expression = "${bufferParam.name}.remaining()"
		val factor = param[AutoSize].factor
		if ( applyFactor && factor != null )
			expression += " ${factor.expression()}"

		if ( bufferParam has nullable )
			expression = "${bufferParam.name} == null ? 0 : $expression"

		if ( (param.nativeType.mapping as PrimitiveMapping).bytes < 4 )
			expression = "(${param.nativeType.javaMethodType})($expression)"

		return expression
	}
}

private fun AutoSizeTransform(bufferParam: Parameter, byteShift: String) =
	if ( byteShift == "0" ) AutoSizeTransform(bufferParam) else AutoSizeBytesTransform(bufferParam, byteShift)

private class AutoSizeBytesTransform(bufferParam: Parameter, val byteShift: String): AutoSizeTransform(bufferParam) {
	override fun transformCall(param: Parameter, original: String): String {
		var expression = "${bufferParam.name}.remaining()"
		val factor = param[AutoSize].factor
		if ( factor != null )
			expression = "($expression ${factor.expression()})"

		// Replace with expression
		return if ( bufferParam has nullable )
			"(${bufferParam.name} == null ? 0 : $expression) << $byteShift"
		else
			"$expression << $byteShift"
	}
}

private open class AutoSizeCharSequenceTransform(val bufferParam: Parameter): FunctionTransform<Parameter> {<commit_msg>Short support in AutoSizeBytesTransform<commit_after>package org.lwjgl.generator

import java.io.PrintWriter

private trait FunctionTransform<T: QualifiedType> {
	fun transformDeclaration(param: T, original: String): String?
	fun transformCall(param: T, original: String): String
}

/** A function transform that must perform some pre-processing. */
private trait PreFunctionTransform<T: QualifiedType> {
	fun preprocess(qtype: T, writer: PrintWriter)
}

/** A function transform that makes use of the APIBuffer. */
private trait APIBufferFunctionTransform<T: QualifiedType> {
	fun setupAPIBuffer(func: Function, qtype: T, writer: PrintWriter)
}

/** Marker trait to indicate that buffer checks should be skipped. */
private trait SkipCheckFunctionTransform

private fun <T: QualifiedType> T.transformDeclarationOrElse(transforms: Map<QualifiedType, FunctionTransform<out QualifiedType>>, original: String): String? {
	val transform = transforms[this]
	if ( transform == null )
		return original
	else
		return [suppress("UNCHECKED_CAST")](transform as FunctionTransform<T>).transformDeclaration(this, original)
}

private fun <T: QualifiedType> T.transformCallOrElse(transforms: Map<QualifiedType, FunctionTransform<out QualifiedType>>, original: String): String {
	val transform = transforms[this]
	if ( transform == null )
		return original
	else
		return [suppress("UNCHECKED_CAST")](transform as FunctionTransform<T>).transformCall(this, original)
}

private open class AutoSizeTransform(val bufferParam: Parameter, val applyFactor: Boolean = true): FunctionTransform<Parameter> {
	override fun transformDeclaration(param: Parameter, original: String) = null // Remove the parameter
	override fun transformCall(param: Parameter, original: String): String {
		var expression = "${bufferParam.name}.remaining()"
		val factor = param[AutoSize].factor
		if ( applyFactor && factor != null )
			expression += " ${factor.expression()}"

		if ( bufferParam has nullable )
			expression = "${bufferParam.name} == null ? 0 : $expression"

		if ( (param.nativeType.mapping as PrimitiveMapping).bytes < 4 )
			expression = "(${param.nativeType.javaMethodType})($expression)"

		return expression
	}
}

private fun AutoSizeTransform(bufferParam: Parameter, byteShift: String) =
	if ( byteShift == "0" ) AutoSizeTransform(bufferParam) else AutoSizeBytesTransform(bufferParam, byteShift)

private class AutoSizeBytesTransform(bufferParam: Parameter, val byteShift: String): AutoSizeTransform(bufferParam) {
	override fun transformCall(param: Parameter, original: String): String {
		var expression = "${bufferParam.name}.remaining()"
		val factor = param[AutoSize].factor
		if ( factor != null )
			expression = "($expression ${factor.expression()})"

		// Replace with expression
		expression = if ( bufferParam has nullable )
			"(${bufferParam.name} == null ? 0 : $expression) << $byteShift"
		else
			"$expression << $byteShift"

		if ( (param.nativeType.mapping as PrimitiveMapping).bytes < 4 )
			expression = "(${param.nativeType.javaMethodType})($expression)"

		return expression
	}
}

private open class AutoSizeCharSequenceTransform(val bufferParam: Parameter): FunctionTransform<Parameter> {
{'fraction_non_alphanumeric': 0.11096774193548387, 'fraction_numerical': 0.0014516129032258066, 'mean_word_length': 5.951793721973094, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '7554904', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1658, 'n_words': 494}