Repository: duckdb/duckdb
File: src/common/types/bit.cpp (C++)

Code before:
	for (idx_t bit_idx = padding; bit_idx < 8; bit_idx++) {
		output[output_idx++] = data[1] & (1 << (7 - bit_idx)) ? '1' : '0';
	}
	for (idx_t byte_idx = 2; byte_idx < len; byte_idx++) {
		for (idx_t bit_idx = 0; bit_idx < 8; bit_idx++) {
			output[output_idx++] = data[byte_idx] & (1 << (7 - bit_idx)) ? '1' : '0';
		}
	}
}

string Bit::ToString(bitstring_t str) {
	auto len = BitLength(str);
	auto buffer = make_unsafe_uniq_array_uninitialized<char>(len);
	ToString(str, buffer.get());
	return string(buffer.get(), len);
}

bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_message) {
	auto data = const_data_ptr_cast(str.GetData());
	auto len = str.GetSize();
	idx_t bit_count = 0;

	if (data[0] == 'x') {
		// Get Bit String Size from hex string
		for (idx_t i = 1; i < len; i++) {
			if (IsValidHexChar(data[i])) {
				bit_count += 4;
			} else {
				string error = StringUtil::Format("Invalid character encountered in hex string -> bit conversion: '%s'",
				                                  string(const_char_ptr_cast(data) + i, 1));
				HandleCastError::AssignError(error, error_message);
				return false;
			}
		}
	} else {
		for (idx_t i = 0; i < len; i++) {
			if (data[i] == '0' || data[i] == '1') {
				bit_count++;
			} else {
				string error = StringUtil::Format("Invalid character encountered in string -> bit conversion: '%s'",
				                                  string(const_char_ptr_cast(data) + i, 1));
				HandleCastError::AssignError(error, error_message);
				return false;
			}
		}
	}

	if (bit_count == 0) {
		string error = "Cannot cast empty string to BIT";
		HandleCastError::AssignError(error, error_message);
		return false;

Review comment (suggestion):
If we add checks in `GetByteFromHex` this can just be `4 * (len - 1)` instead of requiring a loop

Code after:
	Bit::Finalize(target);
}

void Bit::SetEmptyBitString(bitstring_t &target, idx_t len) {
	char *res_buf = target.GetDataWriteable();
	memset(res_buf, 0, target.GetSize());
	res_buf[0] = ComputePadding(len);
	Bit::Finalize(target);
}

// **** casting functions ****
void Bit::ToString(bitstring_t bits, char *output) {
	auto data = const_data_ptr_cast(bits.GetData());
	auto len = bits.GetSize();

	idx_t padding = GetBitPadding(bits);
	idx_t output_idx = 0;
	for (idx_t bit_idx = padding; bit_idx < 8; bit_idx++) {
		output[output_idx++] = data[1] & (1 << (7 - bit_idx)) ? '1' : '0';
	}
	for (idx_t byte_idx = 2; byte_idx < len; byte_idx++) {
		for (idx_t bit_idx = 0; bit_idx < 8; bit_idx++) {
			output[output_idx++] = data[byte_idx] & (1 << (7 - bit_idx)) ? '1' : '0';
		}
	}
}

string Bit::ToString(bitstring_t str) {
	auto len = BitLength(str);
	auto buffer = make_unsafe_uniq_array_uninitialized<char>(len);
	ToString(str, buffer.get());
	return string(buffer.get(), len);
}

bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_message) {
	auto data = const_data_ptr_cast(str.GetData());
	auto len = str.GetSize();
	idx_t bit_count = 0;

	if (data[0] == 'x') {
		// Get Bit String Size from hex string
		bit_count = 4 * (len - 1);
	} else {
		bit_count = len;
	}

	if (bit_count == 0) {
		string error = "Cannot cast empty string to BIT";
		HandleCastError::AssignError(error, error_message);
		return false;
	}