From b3c64946051e90924054f0ec721092dbfbcda1f2 Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Sun, 11 Mar 2012 16:10:42 -0600 Subject: [PATCH] recognize fraction slash as well as regular slash to make fractions --- smartypants.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/smartypants.go b/smartypants.go index 589feb0..797de91 100644 --- a/smartypants.go +++ b/smartypants.go @@ -256,6 +256,7 @@ func smartBacktick(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar byte, text []byte) int { if wordBoundary(previousChar) && len(text) >= 3 { // is it of the form digits/digits(word boundary)?, i.e., \d+/\d+\b + // note: check for regular slash (/) or fraction slash (⁄, 0x2044, or 0xe2 81 84 in utf-8) numEnd := 0 for len(text) > numEnd && isdigit(text[numEnd]) { numEnd++ @@ -264,15 +265,18 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b out.WriteByte(text[0]) return 0 } - if len(text) < numEnd+2 || text[numEnd] != '/' { + denStart := numEnd + 1 + if len(text) > numEnd+3 && text[numEnd] == 0xe2 && text[numEnd+1] == 0x81 && text[numEnd+2] == 0x84 { + denStart = numEnd + 3 + } else if len(text) < numEnd+2 || text[numEnd] != '/' { out.WriteByte(text[0]) return 0 } - denEnd := numEnd + 1 + denEnd := denStart for len(text) > denEnd && isdigit(text[denEnd]) { denEnd++ } - if denEnd == numEnd+1 { + if denEnd == denStart { out.WriteByte(text[0]) return 0 } @@ -280,7 +284,7 @@ func smartNumberGeneric(out *bytes.Buffer, smrt *smartypantsData, previousChar b out.WriteString("") out.Write(text[:numEnd]) out.WriteString("") - out.Write(text[numEnd+1 : denEnd]) + out.Write(text[denStart:denEnd]) out.WriteString("") return denEnd - 1 }