FIXED: CPAttributedString is unstable and jumbles attributes during editing

The current implementation does not work correctly in several corner cases.
These issues are rectified by this pull request.
I.e. my new CPTextView will not work without these patches.