syntax coloring...

Discussion in 'Mac Programming' started by varsis, Oct 14, 2008.

  1. varsis macrumors regular

    Joined:
    Nov 30, 2005
    #1
    Hi,

    I currently use the textContainer delegate to color my NSTextView. But The problem being it is VERY slow. I use regex to find the right tags but when loading large files it takes forever. I am using css coloring. I tried it in CSS edit and it loaded extremely fast.

    What I am looking for is a very fast way to load the right syntax...

    Thanks
    Chris
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    Did you try sampling your own coloring scheme to find the bottleneck?
     
  3. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #3
    What do you mean?
     
  4. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    Run Shark or Instruments or 'sample' on your own code to find what's causing the slow down.
     
  5. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #5
    Il try that but I don't think it's the code but it's the method, like it's not that SLOW but it's slow enough and when compared to css edit coloring it it's incredibly slow.. 2-3 second to syntax a 60,000 characters... Using regex methods...

    Chris
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    That's why you use Shark, so that you don't "think" what is slow but you _know_.
     
  7. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #7
    Never Used shark and don't know how but Il give it a try but here is the code VERY simple.

    Code:
    
    	NSString *regexStringProps   = @"[A-Za-z0-9-#.>:., _*]+\\:"; 
    	RKEnumerator *matchEnumerator = [string matchEnumeratorWithRegex:regexStringProps];
    	while([matchEnumerator nextRanges] != NULL) {
    		//NSLog(@"%@ Each Peice",NSStringFromRange([matchEnumerator currentRange]));
    		NSRange propsRange = [matchEnumerator currentRange];
    		propsRange.length = propsRange.length-1;
    		[textStorage addAttribute:NSForegroundColorAttributeName
    							value:orange
    							range:propsRange];	
    	}
    
    The above matches code before the ":" in css lines.
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    There is absolutely no point in looking at the code if you haven't done any measuring. It's a waste of time. To use Shark: Start Shark. Select the application to profile. Click on "Start". It couldn't be any easier.
     
  9. tyr2 macrumors 6502a

    tyr2

    Joined:
    May 6, 2006
    Location:
    Leeds, UK
    #9
    Ok I don't know how RKEnumerator works or entirely what you're tying to do here but couldn't you replace [A-Za-z0-9-#.>:., _*]+ with \S+ (i.e. match non white space). Which would be quicker to process that a bunch of ranges.

    Just a thought.
     
  10. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #10
    Used shark didn't see anything incriminating... The function seemed to run fine according to shark.
     
  11. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #11
    Shark doesn't tell you whether it ran fine or not. It tells you how much time was spent on each line, each function, each asm instruction, etc... with tools for digging into that information and organizing it.
     
  12. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #12
    Found the Solutions, Turns out Regex Kit uses PCRE to find issues with the Regex commands. But PCRE only supports only UTF-8 But the work around it to use ASCII encoding, which allows your code to run more or less in either UTF-8 or UTF-16 without conversion. Would convert UTF-8 to UTF-16 to UTF-8.

    Using ACSII it doesn't need to do this.

    Code:
    NSString *string = [NSString stringWithCString:[**PLACE YOUR NSSTRING HERE** cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSASCIIStringEncoding];
    Does the Trick now loads each regex in about 3 thousandths of a second. Versus the 2 seconds.

    Here is a article on this: http://lists.apple.com/archives/xcode-users/2008/Mar/msg00329.html
     

Share This Page