Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Code to determine whether the passed in string is a C style comment block - in T-SQL

Code to determine whether the passed in string is a C style comment block - in T-SQL

Scheduled Pinned Locked Moved The Weird and The Wonderful
databaseregex
7 Posts 5 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • honey the codewitchH Online
    honey the codewitchH Online
    honey the codewitch
    wrote on last edited by
    #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
    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 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
    
    J M J 3 Replies Last reply
    0
    • honey the codewitchH honey the codewitch

      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
      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 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
      
      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      UTF16 ain't good enough for you?

      Wrong is evil and must be defeated. - Jeff Ello

      honey the codewitchH 1 Reply Last reply
      0
      • honey the codewitchH honey the codewitch

        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
        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 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
        
        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        code salad.

        CI/CD = Continuous Impediment/Continuous Despair

        B 1 Reply Last reply
        0
        • J Jorgen Andersson

          UTF16 ain't good enough for you?

          Wrong is evil and must be defeated. - Jeff Ello

          honey the codewitchH Online
          honey the codewitchH Online
          honey the codewitch
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Maximilien

            code salad.

            CI/CD = Continuous Impediment/Continuous Despair

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #5

            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???

            1 Reply Last reply
            0
            • honey the codewitchH honey the codewitch

              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
              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 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
              
              J Offline
              J Offline
              James Curran
              wrote on last edited by
              #6

              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

              honey the codewitchH 1 Reply Last reply
              0
              • J James Curran

                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

                honey the codewitchH Online
                honey the codewitchH Online
                honey the codewitch
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups