// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.// https://github.com/sergi/go-diff// See the included LICENSE file for license details.//// go-diff is a Go implementation of Google's Diff, Match, and Patch library// Original library is Copyright (c) 2006 Google Inc.// http://code.google.com/p/google-diff-match-patch/package diffmatchpatchimport ()constUNICODE_INVALID_RANGE_START = 0xD800constUNICODE_INVALID_RANGE_END = 0xDFFFconstUNICODE_INVALID_RANGE_DELTA = UNICODE_INVALID_RANGE_END - UNICODE_INVALID_RANGE_START + 1constUNICODE_RANGE_MAX = 0x10FFFF// unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.// In speed critical applications this could be dropped since the receiving application will certainly decode these fine. Note that this function is case-sensitive. Thus "%3F" would not be unescaped. But this is ok because it is only called with the output of HttpUtility.UrlEncode which returns lowercase hex. Example: "%3f" -> "?", "%24" -> "$", etc.var unescaper = strings.NewReplacer("%21", "!", "%7E", "~", "%27", "'","%28", "(", "%29", ")", "%3B", ";","%2F", "/", "%3F", "?", "%3A", ":","%40", "@", "%26", "&", "%3D", "=","%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")// indexOf returns the first index of pattern in str, starting at str[i].func indexOf( string, string, int) int {if > len()-1 {return -1 }if <= 0 {returnstrings.Index(, ) } := strings.Index([:], )if == -1 {return -1 }return + }// lastIndexOf returns the last index of pattern in str, starting at str[i].func lastIndexOf( string, string, int) int {if < 0 {return -1 }if >= len() {returnstrings.LastIndex(, ) } , := utf8.DecodeRuneInString([:])returnstrings.LastIndex([:+], )}// runesIndexOf returns the index of pattern in target, starting at target[i].func runesIndexOf(, []rune, int) int {if > len()-1 {return -1 }if <= 0 {returnrunesIndex(, ) } := runesIndex([:], )if == -1 {return -1 }return + }func runesEqual(, []rune) bool {iflen() != len() {returnfalse }for , := range {if != [] {returnfalse } }returntrue}// runesIndex is the equivalent of strings.Index for rune slices.func runesIndex(, []rune) int { := len() - len()for := 0; <= ; ++ {ifrunesEqual([:+len()], ) {return } }return -1}func intArrayToString( []index) string {iflen() == 0 {return"" } := []rune{}for , := range { = append(, intToRune(uint32())) }returnstring()}// These constants define the number of bits representable// in 1,2,3,4 byte utf8 sequences, respectively.constONE_BYTE_BITS = 7constTWO_BYTE_BITS = 11constTHREE_BYTE_BITS = 16constFOUR_BYTE_BITS = 21// Helper for getting a sequence of bits from an integer.func getBits( uint32, byte, byte) byte {returnbyte(( >> ) & ((1 << ) - 1))}// Converts an integer in the range 0~1112060 into a rune.// Based on the ranges table in https://en.wikipedia.org/wiki/UTF-8func intToRune( uint32) rune {if < (1 << ONE_BYTE_BITS) {returnrune() }if < (1 << TWO_BYTE_BITS) { , := utf8.DecodeRune([]byte{0b11000000 | getBits(, 5, 6), 0b10000000 | getBits(, 6, 0)})if != 2 || == utf8.RuneError {panic(fmt.Sprintf("Error encoding an int %d with size 2, got rune %v and size %d", , , )) }return }// Last -3 here needed because for some reason 3rd to last codepoint 65533 in this range // was returning utf8.RuneError during encoding.if < ((1 << THREE_BYTE_BITS) - UNICODE_INVALID_RANGE_DELTA - 3) {if >= UNICODE_INVALID_RANGE_START { += UNICODE_INVALID_RANGE_DELTA } , := utf8.DecodeRune([]byte{0b11100000 | getBits(, 4, 12), 0b10000000 | getBits(, 6, 6), 0b10000000 | getBits(, 6, 0)})if != 3 || == utf8.RuneError {panic(fmt.Sprintf("Error encoding an int %d with size 3, got rune %v and size %d", , , )) }return }if < (1<<FOUR_BYTE_BITS - UNICODE_INVALID_RANGE_DELTA - 3) { += UNICODE_INVALID_RANGE_DELTA + 3 , := utf8.DecodeRune([]byte{0b11110000 | getBits(, 3, 18), 0b10000000 | getBits(, 6, 12), 0b10000000 | getBits(, 6, 6), 0b10000000 | getBits(, 6, 0)})if != 4 || == utf8.RuneError {panic(fmt.Sprintf("Error encoding an int %d with size 4, got rune %v and size %d", , , )) }return }panic(fmt.Sprintf("The integer %d is too large for runeToInt()", ))}// Converts a rune generated by intToRune back to an integerfunc runeToInt( rune) uint32 { := uint32()if < (1 << ONE_BYTE_BITS) {return } := []byte{0, 0, 0, 0} := utf8.EncodeRune(, )if == 2 {returnuint32([0]&0b11111)<<6 | uint32([1]&0b111111) }if == 3 { := uint32([0]&0b1111)<<12 | uint32([1]&0b111111)<<6 | uint32([2]&0b111111)if >= UNICODE_INVALID_RANGE_END {return - UNICODE_INVALID_RANGE_DELTA }return }if == 4 { := uint32([0]&0b111)<<18 | uint32([1]&0b111111)<<12 | uint32([2]&0b111111)<<6 | uint32([3]&0b111111)return - UNICODE_INVALID_RANGE_DELTA - 3 }panic(fmt.Sprintf("Unexpected state decoding rune=%v size=%d", , ))}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.