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. +1 -1 code patch.... or how to make my brain explode....

+1 -1 code patch.... or how to make my brain explode....

Scheduled Pinned Locked Moved The Weird and The Wonderful
data-structureshelptutorialquestionannouncement
6 Posts 2 Posters 2 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.
  • A Offline
    A Offline
    Alberto Bar Noy
    wrote on last edited by
    #1

    Consider a text box with auto complete checking for the existence of the char . or [ Brilliant outsource developer sitting far far away writes the following:

    private TextOffsets getTextOffsets(string text, int caretIndex)
    {
    var heirarchyChars = HeirarchyChars.ToArray();

    int previousSeparatorOffset = -1;
    
    if (caretIndex > 0)
    {
    	previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, caretIndex);
    }
    

    ...

    And of course commits the change... 10 minutes after update contracts kick-in in run time and

    ContractException was unhandled
    Precondition failed: this == String.Empty || startIndex - count >= 0

    Then I go :mad::mad::mad: Update and get this

    private TextOffsets getTextOffsets(string text, int caretIndex)
    {
    var heirarchyChars = HeirarchyChars.ToArray();

    int previousSeparatorOffset = -1;
    try
    {
    	if (caretIndex > 0)
    	{
    		previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, caretIndex);
    	}
    
    }
    catch (Exception x)
    {
    
        }
    

    ...

    Absolutely brilliant. Let's not fix it.. let's circumvent it... :wtf: :wtf: By this time curses in Greek are being launched... Next day update and...

    private TextOffsets getTextOffsets(string text, int caretIndex)
    {
    var heirarchyChars = HeirarchyChars.ToArray();

    int previousSeparatorOffset = -1;
    int count = caretIndex - 2;
    if (caretIndex > 0 && count > 0 )
    {
    	previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, count);
    }
    

    ...

    :doh: :doh: what the... Pen and paper just to make sure I am not hallucinating... yep we always go back one to check one character forward... Also we have an innovation... one would start an expression for a struct or an array with a . or a [ from position 0 without a variable first. Just brilliant... :wtf: :wtf: Oh well... it should at least be...

    private TextOffsets getTextOffsets(string text, int caretIndex)
    {
    var heirarchyChars = HeirarchyChars.ToArray();

    int previousSeparatorOffset = -1;
    
    if (caretIndex > 1)
    {
    	previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, 1);
    }
    

    ...

    Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

    K 1 Reply Last reply
    0
    • A Alberto Bar Noy

      Consider a text box with auto complete checking for the existence of the char . or [ Brilliant outsource developer sitting far far away writes the following:

      private TextOffsets getTextOffsets(string text, int caretIndex)
      {
      var heirarchyChars = HeirarchyChars.ToArray();

      int previousSeparatorOffset = -1;
      
      if (caretIndex > 0)
      {
      	previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, caretIndex);
      }
      

      ...

      And of course commits the change... 10 minutes after update contracts kick-in in run time and

      ContractException was unhandled
      Precondition failed: this == String.Empty || startIndex - count >= 0

      Then I go :mad::mad::mad: Update and get this

      private TextOffsets getTextOffsets(string text, int caretIndex)
      {
      var heirarchyChars = HeirarchyChars.ToArray();

      int previousSeparatorOffset = -1;
      try
      {
      	if (caretIndex > 0)
      	{
      		previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, caretIndex);
      	}
      
      }
      catch (Exception x)
      {
      
          }
      

      ...

      Absolutely brilliant. Let's not fix it.. let's circumvent it... :wtf: :wtf: By this time curses in Greek are being launched... Next day update and...

      private TextOffsets getTextOffsets(string text, int caretIndex)
      {
      var heirarchyChars = HeirarchyChars.ToArray();

      int previousSeparatorOffset = -1;
      int count = caretIndex - 2;
      if (caretIndex > 0 && count > 0 )
      {
      	previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, count);
      }
      

      ...

      :doh: :doh: what the... Pen and paper just to make sure I am not hallucinating... yep we always go back one to check one character forward... Also we have an innovation... one would start an expression for a struct or an array with a . or a [ from position 0 without a variable first. Just brilliant... :wtf: :wtf: Oh well... it should at least be...

      private TextOffsets getTextOffsets(string text, int caretIndex)
      {
      var heirarchyChars = HeirarchyChars.ToArray();

      int previousSeparatorOffset = -1;
      
      if (caretIndex > 1)
      {
      	previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, 1);
      }
      

      ...

      Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

      K Offline
      K Offline
      KP Lee
      wrote on last edited by
      #2

      Nice development environment you have there. Somebody offshore can commit a change that immediately hits production without going through test environment, not bothering with code review, your offshore idiot remains hired... (You are saving $ not paying somebody local, breakdowns of your application reduces costs of running the system for no customers because it doesn't work...) I'm not too sure about you...

      Alberto Bar-Noy wrote:

      yep we always go back one to check one character forward...

      That's for?:

      Alberto Bar-Noy wrote:

      previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, count);

      If caretIndex==10, count would be 8, the start of looking in the array is at 9. If text is a 5 character string, you are allowing the search to start 5 characters past the first valid index location. Same situation, except the string is 10 characters long. You can only look 1 character ahead, yet you are asking for 8, that will blow with the original error you got. (The string has to be 18 characters long or longer for that to work.) Your statement to look only 1 character forward doesn't make sense either. Going from where you start to the end of the string makes sense to me. Why you are going back 1 character from the index passed doesn't make sense to me either. I'd want to use the last index found to find the next index to be found. Here's an alternate version:

      private TextOffsets getTextOffsets(string text, int caretIndex)
      {
      var heirarchyChars = HeirarchyChars.ToArray();//one upper case to lower makes it really obvious it's new.
      var cnt = heirarchyChars.Count();//total length of the string to be searched.
      var strt = caretIndex + 1;//Point to the NEXT index's starting point
      // change "+" to "-" if I'm all wet
      int NewIndex = -1;

      if (strt < cnt)// make sure at least ONE character is searched
      {
      	NewIndex = text.LastIndexOfAny(heirarchyChars, strt, cnt - strt);
      }
      

      Pass in 9 for the starting index for a 10 character string, cnt is 10, strt is 10, doesn't check the string at all. Of course, since I'm perfect, :laugh: I'll go ahead and check this in without even seeing if it will compile...

      A 1 Reply Last reply
      0
      • K KP Lee

        Nice development environment you have there. Somebody offshore can commit a change that immediately hits production without going through test environment, not bothering with code review, your offshore idiot remains hired... (You are saving $ not paying somebody local, breakdowns of your application reduces costs of running the system for no customers because it doesn't work...) I'm not too sure about you...

        Alberto Bar-Noy wrote:

        yep we always go back one to check one character forward...

        That's for?:

        Alberto Bar-Noy wrote:

        previousSeparatorOffset = text.LastIndexOfAny(heirarchyChars, caretIndex - 1, count);

        If caretIndex==10, count would be 8, the start of looking in the array is at 9. If text is a 5 character string, you are allowing the search to start 5 characters past the first valid index location. Same situation, except the string is 10 characters long. You can only look 1 character ahead, yet you are asking for 8, that will blow with the original error you got. (The string has to be 18 characters long or longer for that to work.) Your statement to look only 1 character forward doesn't make sense either. Going from where you start to the end of the string makes sense to me. Why you are going back 1 character from the index passed doesn't make sense to me either. I'd want to use the last index found to find the next index to be found. Here's an alternate version:

        private TextOffsets getTextOffsets(string text, int caretIndex)
        {
        var heirarchyChars = HeirarchyChars.ToArray();//one upper case to lower makes it really obvious it's new.
        var cnt = heirarchyChars.Count();//total length of the string to be searched.
        var strt = caretIndex + 1;//Point to the NEXT index's starting point
        // change "+" to "-" if I'm all wet
        int NewIndex = -1;

        if (strt < cnt)// make sure at least ONE character is searched
        {
        	NewIndex = text.LastIndexOfAny(heirarchyChars, strt, cnt - strt);
        }
        

        Pass in 9 for the starting index for a 10 character string, cnt is 10, strt is 10, doesn't check the string at all. Of course, since I'm perfect, :laugh: I'll go ahead and check this in without even seeing if it will compile...

        A Offline
        A Offline
        Alberto Bar Noy
        wrote on last edited by
        #3

        well this code never reached production' This is what I was hired for... to kick them out. This code tries to check if the last character typed was . or [ this is an autocomplete for structs and arrays on a PLC Dev. system's only the last char needs to be checked. And in WPF the caret position is after the last character. Finally because of politics (they got their notice so they are still around till Jan. first and I can't fully touch their code.

        Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

        K 2 Replies Last reply
        0
        • A Alberto Bar Noy

          well this code never reached production' This is what I was hired for... to kick them out. This code tries to check if the last character typed was . or [ this is an autocomplete for structs and arrays on a PLC Dev. system's only the last char needs to be checked. And in WPF the caret position is after the last character. Finally because of politics (they got their notice so they are still around till Jan. first and I can't fully touch their code.

          Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

          K Offline
          K Offline
          KP Lee
          wrote on last edited by
          #4

          Why pass an index field at all? Get the count, subtract 1, use that as the first integer and 1 as your second. If you can't get rid of the field, ignore it and do the same thing. Don't know enough about WPF, but a string is a string and the caret is either the last character, earlier than the last, or doesn't exist in the string.

          A 1 Reply Last reply
          0
          • A Alberto Bar Noy

            well this code never reached production' This is what I was hired for... to kick them out. This code tries to check if the last character typed was . or [ this is an autocomplete for structs and arrays on a PLC Dev. system's only the last char needs to be checked. And in WPF the caret position is after the last character. Finally because of politics (they got their notice so they are still around till Jan. first and I can't fully touch their code.

            Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

            K Offline
            K Offline
            KP Lee
            wrote on last edited by
            #5

            Sorry, it's late, I'm not thinking straight. I can't think of a way to find the length of a string other than text.ToCharArray.Count()

            1 Reply Last reply
            0
            • K KP Lee

              Why pass an index field at all? Get the count, subtract 1, use that as the first integer and 1 as your second. If you can't get rid of the field, ignore it and do the same thing. Don't know enough about WPF, but a string is a string and the caret is either the last character, earlier than the last, or doesn't exist in the string.

              A Offline
              A Offline
              Alberto Bar Noy
              wrote on last edited by
              #6

              I know! Politics! I can't touch it yet :-(

              Alberto Bar-Noy --------------- “The city’s central computer told you? R2D2, you know better than to trust a strange computer!” (C3PO)

              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