Code to determine whether the passed in string is a C style comment block - in T-SQL
-
In my defense this code was generated by a tool. It's still some of the weirdest code to do string ops in SQL I've seen. Part of it is due to the fact that it converts everything to UTF32 as it goes, even though in this routine it really doesn't need to.
-- Matches the block end for CommentBlock
DROP PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd]
GO
CREATE PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd] @value NVARCHAR(MAX), @index INT, @ch INT
AS
BEGIN
DECLARE @adv INT
DECLARE @matched INT
DECLARE @valueEnd INT = DATALENGTH(@value)/2+1
DECLARE @tch BIGINT
DECLARE @accept INT = -1
DECLARE @result INT = 0
WHILE @ch <> -1
BEGIN
SET @matched = 0
-- q0
IF @ch = 42
BEGINSET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q1 END -- IF {range match} GOTO next q1: IF @ch = 47 BEGIN SET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q2 END -- IF {range match} GOTO next q2: RETURN CASE @ch WHEN -1 THEN 1 ELSE 0 END next: IF @matched = 0 BEGIN SET @index = @index + 1
-
In my defense this code was generated by a tool. It's still some of the weirdest code to do string ops in SQL I've seen. Part of it is due to the fact that it converts everything to UTF32 as it goes, even though in this routine it really doesn't need to.
-- Matches the block end for CommentBlock
DROP PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd]
GO
CREATE PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd] @value NVARCHAR(MAX), @index INT, @ch INT
AS
BEGIN
DECLARE @adv INT
DECLARE @matched INT
DECLARE @valueEnd INT = DATALENGTH(@value)/2+1
DECLARE @tch BIGINT
DECLARE @accept INT = -1
DECLARE @result INT = 0
WHILE @ch <> -1
BEGIN
SET @matched = 0
-- q0
IF @ch = 42
BEGINSET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q1 END -- IF {range match} GOTO next q1: IF @ch = 47 BEGIN SET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q2 END -- IF {range match} GOTO next q2: RETURN CASE @ch WHEN -1 THEN 1 ELSE 0 END next: IF @matched = 0 BEGIN SET @index = @index + 1
UTF16 ain't good enough for you?
Wrong is evil and must be defeated. - Jeff Ello
-
In my defense this code was generated by a tool. It's still some of the weirdest code to do string ops in SQL I've seen. Part of it is due to the fact that it converts everything to UTF32 as it goes, even though in this routine it really doesn't need to.
-- Matches the block end for CommentBlock
DROP PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd]
GO
CREATE PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd] @value NVARCHAR(MAX), @index INT, @ch INT
AS
BEGIN
DECLARE @adv INT
DECLARE @matched INT
DECLARE @valueEnd INT = DATALENGTH(@value)/2+1
DECLARE @tch BIGINT
DECLARE @accept INT = -1
DECLARE @result INT = 0
WHILE @ch <> -1
BEGIN
SET @matched = 0
-- q0
IF @ch = 42
BEGINSET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q1 END -- IF {range match} GOTO next q1: IF @ch = 47 BEGIN SET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q2 END -- IF {range match} GOTO next q2: RETURN CASE @ch WHEN -1 THEN 1 ELSE 0 END next: IF @matched = 0 BEGIN SET @index = @index + 1
code salad.
CI/CD = Continuous Impediment/Continuous Despair
-
UTF16 ain't good enough for you?
Wrong is evil and must be defeated. - Jeff Ello
UTF16 surrogate pairs exist, so no. Frankly, I wish UTF16 didn't exist at all. It seems a waste of everyone's time. No wonder MS adopted it.
Real programmers use butterflies
-
code salad.
CI/CD = Continuous Impediment/Continuous Despair
With a side of syntax soup.
What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???
-
In my defense this code was generated by a tool. It's still some of the weirdest code to do string ops in SQL I've seen. Part of it is due to the fact that it converts everything to UTF32 as it goes, even though in this routine it really doesn't need to.
-- Matches the block end for CommentBlock
DROP PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd]
GO
CREATE PROCEDURE [dbo].[SqlCompiledChecker_IsCommentBlockBlockEnd] @value NVARCHAR(MAX), @index INT, @ch INT
AS
BEGIN
DECLARE @adv INT
DECLARE @matched INT
DECLARE @valueEnd INT = DATALENGTH(@value)/2+1
DECLARE @tch BIGINT
DECLARE @accept INT = -1
DECLARE @result INT = 0
WHILE @ch <> -1
BEGIN
SET @matched = 0
-- q0
IF @ch = 42
BEGINSET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q1 END -- IF {range match} GOTO next q1: IF @ch = 47 BEGIN SET @index = @index + 1 SET @adv = 1 IF @index < @valueEnd BEGIN SET @ch = UNICODE(SUBSTRING(@value, @index, 1)) SET @tch = @ch - 0xd800 IF @tch < 0 SET @tch = @tch + 2147483648 IF @tch < 2048 BEGIN SET @ch = @ch \* 1024 SET @index = @index + 1 SET @adv = 2 IF @index >= @valueEnd RETURN -1 SET @ch = @ch + UNICODE(SUBSTRING(@value, @index, 1)) - 0x35fdc00 END END ELSE BEGIN SET @ch = -1 END SET @matched = 1 GOTO q2 END -- IF {range match} GOTO next q2: RETURN CASE @ch WHEN -1 THEN 1 ELSE 0 END next: IF @matched = 0 BEGIN SET @index = @index + 1
I'm dying to know what sin in a past life led you to NEED to determine whether the passed in string is a C style comment block - in T-SQL.
Truth, James
-
I'm dying to know what sin in a past life led you to NEED to determine whether the passed in string is a C style comment block - in T-SQL.
Truth, James
The comment block was test code. The error of my ways was an outdated idea of restricting access to tables in SQL coupled with stored procedure based field validation, which is where this came in. There is a code generator that produced this particular matcher.
Real programmers use butterflies