HTML Encoder in Pasteboard

Before posting more, I needed an html encoder in Pasteboard to change <,>,&,… into <,>&… before pasting into in <pre>code-here</pre>.

I copy text/code into Pasteboard by cmd+c, run the program and cmd+v to paste the encoded string.

AppDelegate.m

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSPasteboard*  pasteBoard  = [NSPasteboard generalPasteboard];
    // get string from the pasteboard
    NSString* copiedString = [pasteBoard stringForType:NSPasteboardTypeString];
    // html encode
    NSString* encodedString = [NSString HtmlEncode:copiedString];
    // copy it into the pasteboard
    [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil];
    [pasteBoard setString:encodedString forType:NSStringPboardType];
    
    // quit the app
    [[NSApplication sharedApplication] terminate:self];
}

NSString+HTML.h/.m

#import <Foundation/Foundation.h>

@interface NSString (HTML)

+ (NSString*)HtmlEncode:(NSString*)htmlString;

@end

#import "NSString+HTML.h"

@implementation NSString (HTML)

+ (NSString*)HtmlEncode:(NSString*)htmlString {
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&"  withString:@"&amp;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<"  withString:@"&lt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@">"  withString:@"&gt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"""" withString:@"&quot;"];    
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'"  withString:@"&#039;"];
    return htmlString;
}

@end

Automatic Reference Counting

Tested __strong, __weak and __unsafe_unretained.
__weak reference becomes nil when the target is dealloced, __unsafe_unretained becomes a dangling pointer.

Test Result:
1. Alloc -> Print

2012-06-14 23:49:08.286 ArcSpike[1561:403] strong: <NSObject: 0x7f8813b225c0>
2012-06-14 23:49:08.287 ArcSpike[1561:403] weak:   <NSObject: 0x7f8813b225c0>
2012-06-14 23:49:08.287 ArcSpike[1561:403] unsafe: <NSObject: 0x7f8813b225c0>

2. Alloc -> Free -> Print

2012-06-14 23:49:09.253 ArcSpike[1561:403] strong: (null)
2012-06-14 23:49:09.254 ArcSpike[1561:403] weak:   (null)
2012-06-14 23:49:09.255 ArcSpike[1561:403] unsafe: <NSObject: 0x7f8813b225c0>

or, causes access violation when referencing unsafeReference in print.

Test Code:

@interface WindowController : NSWindowController
{
id __strong strongReference;
id __weak weakReference;
id __unsafe_unretained unsafeReference;
}

- (IBAction)testAlloc:(id)sender;
- (IBAction)testFree:(id)sender;
- (IBAction)print:(id)sender;

@end

@implementation WindowController
...

- (IBAction)testAlloc:(id)sender
{
strongReference = [[NSObject alloc] init];
weakReference = strongReference;
unsafeReference = strongReference;
}

- (IBAction)testFree:(id)sender
{
strongReference = nil;
}

- (IBAction)print:(id)sender
{
NSLog(@"strong: %@", strongReference);
NSLog(@"weak: %@", weakReference);
NSLog(@"unsafe: %@", unsafeReference);
}
@end