Runes
发布者:admin 发表于:417天前 阅读数:614 评论:0

本文整理汇总了Golang中bytes.Runes函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Runes函数的具体用法?Golang Runes怎么用?Golang Runes使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了Runes函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: FixPrematureTransform

/*
FixPrematureTransform - Used by clients to fix incoming and outgoing transforms when local changes
have been applied to a document before being routed through the server.

In order for a client UI to be unblocking it must apply local changes as the user types them before
knowing the correct order of the change. Therefore, it is possible to apply a local change before
receiving incoming transforms that are meant to be applied beforehand.

As a solution to those situations this function allows a client to alter and incoming interations
such that if they were to be applied to the local document after our local change they would result
in the same document. The outgoing transform is also modified for sending out to the server.

It is possible that the local change has already been dispatched to the server, in which case it is
the servers responsibility to fix the transform so that other clients end up at the same result.

NOTE: These fixes do not regard or alter the versions of either transform.
*/
func FixPrematureTransform(unapplied, unsent *OTransform) {
    var before, after *OTransform

    // Order the OTs by position in the document.
    if unapplied.Position < unsent.Position {
        before = unapplied
        after = unsent
    } else {
        before = unsent
        after = unapplied
    }

    // Get insertion lengths (codepoints)
    bInsert, aInsert := bytes.Runes([]byte(before.Insert)), bytes.Runes([]byte(after.Insert))
    bLength, aLength := len(bInsert), len(aInsert)

    if before.Delete == 0 {
        after.Position += bLength
    } else if (before.Delete + before.Position) <= after.Position {
        after.Position += (bLength - before.Delete)
    } else {
        posGap := after.Position - before.Position
        excess := intMax(0, before.Delete-posGap)

        if excess > after.Delete {
            before.Delete += (aLength - after.Delete)
            before.Insert = before.Insert + after.Insert
        } else {
            before.Delete = posGap
        }

        after.Delete = intMax(0, after.Delete-excess)
        after.Position = before.Position + bLength
    }
}

开发者ID:Jeffail,项目名称:leaps,代码行数:52,代码来源:transforms.go

示例2: NewBuffer

func NewBuffer(r io.Reader) (Buffer, error) {
    var b Buffer
    b.Lines = make([]Line, 0)

    // bytes are read into this from the reader
    rb := make([]byte, READ_BUFFER_SIZE)
    // bytes are queued here until we find EOL
    lb := make([]byte, 0, READ_BUFFER_SIZE)
    for {
        n, err := r.Read(rb)
        for i := 0; i < n; i++ {
            lb = append(lb, rb[i])
            if rb[i] == '\n' {
                l := Line(bytes.Runes(lb))
                b.Lines = append(b.Lines, l)
                lb = make([]byte, 0, READ_BUFFER_SIZE)
            }
        }
        if err != nil && err == io.EOF {
            lb = append(lb, '\n')
            l := Line(bytes.Runes(lb))
            b.Lines = append(b.Lines, l)
            lb = make([]byte, 0, READ_BUFFER_SIZE)
            break
        } else if err != nil {
            return b, err
        }
    }
    return b, nil
}

开发者ID:haldean,项目名称:xt,代码行数:30,代码来源:io.go

示例3: FixOutOfDateTransform

/*
FixOutOfDateTransform - When a transform created for a specific version is later determined to come
after one or more other transforms it can be fixed. This fix translates the transform such that
being applied in the correct order will preserve the original intention.

In order to apply these fixes this function should be called with the target transform and the
actual versioned transform that the target currently 'believes' it is. So, for example, if the
transform was written for version 7 and was actually 10 you would call FixOutOfDateTransform in this
order:

FixOutOfDateTransform(target, version7)
FixOutOfDateTransform(target, version8)
FixOutOfDateTransform(target, version9)

Once the transform is adjusted through this fix it can be harmlessly dispatched to all other clients
which will end up with the same document as the client that submitted this transform.

NOTE: These fixes do not regard or alter the versions of either transform.
*/
func FixOutOfDateTransform(sub, pre *OTransform) {
    // Get insertion lengths (codepoints)
    subInsert, preInsert := bytes.Runes([]byte(sub.Insert)), bytes.Runes([]byte(pre.Insert))
    subLength, preLength := len(subInsert), len(preInsert)

    if pre.Position <= sub.Position {
        if preLength > 0 && pre.Delete == 0 {
            sub.Position += preLength
        } else if pre.Delete > 0 && (pre.Position+pre.Delete) <= sub.Position {
            sub.Position += (preLength - pre.Delete)
        } else if pre.Delete > 0 && (pre.Position+pre.Delete) > sub.Position {
            overhang := intMin(sub.Delete, (pre.Position+pre.Delete)-sub.Position)
            sub.Delete -= overhang
            sub.Position = pre.Position + preLength
        }
    } else if sub.Delete > 0 && (sub.Position+sub.Delete) > pre.Position {
        posGap := pre.Position - sub.Position
        excess := intMax(0, (sub.Delete - posGap))

        if excess > pre.Delete {
            sub.Delete += (preLength - pre.Delete)

            newInsert := make([]rune, subLength+preLength)
            copy(newInsert[:], subInsert)
            copy(newInsert[subLength:], preInsert)

            sub.Insert = string(newInsert)
        } else {
            sub.Delete = posGap
        }
    }
}

开发者ID:Jeffail,项目名称:leaps,代码行数:51,代码来源:transforms.go

示例4: makeEncoding

func makeEncoding(decode bool) (encoding map[rune]rune) {
    base64Runes := bytes.Runes([]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="))
    encojiRunes := bytes.Runes([]byte(":grinning_face::grimacing_face::beaming_face_with_smiling_eyes::face_with_tears_of_joy::grinning_face_with_big_eyes::grinning_face_with_smiling_eyes::grinning_face_with_sweat::grinning_squinting_face::smiling_face_with_halo::winking_face::smiling_face_with_smiling_eyes::face_savoring_food::relieved_face::smiling_face_with_heart-eyes::face_blowing_a_kiss::kissing_face::kissing_face_with_smiling_eyes::kissing_face_with_closed_eyes::winking_face_with_tongue::squinting_face_with_tongue::face_with_tongue::smiling_face_with_sunglasses::smirking_face::pile_of_poo::ghost::grinning_cat::grinning_cat_with_smiling_eyes::cat_with_tears_of_joy::smiling_cat_with_heart-eyes::dog_face::cat_face::mouse_face::hamster::rabbit_face::bear::panda::koala::tiger_face::cow_face::pig_face::pig_nose::frog::octopus::monkey_face::see-no-evil_monkey::hear-no-evil_monkey::speak-no-evil_monkey::monkey::chicken::penguin::bird::baby_chick::hatching_chick::front-facing_baby_chick::wolf::boar::horse_face::honeybee::bug::snail::lady_beetle::ant::elephant::dolphin::spouting_whale:"))

    if len(base64Runes) != len(encojiRunes) {
        panic("Charsets must be of same length")
    }

    encoding = make(map[rune]rune)

    var from, to []rune

    if decode {
        from = encojiRunes
        to = base64Runes
    } else {
        from = base64Runes
        to = encojiRunes
    }

    for i := 0; i < len(from); i++ {
        encoding[from[i]] = to[i]
    }
    return encoding
}

开发者ID:andrewhamon,项目名称:encoji,代码行数:25,代码来源:encoji.go

示例5: scanIdentifier

func (self *Scanner) scanIdentifier() (*Token, error) {
    var (
        buf  bytes.Buffer
        stop bool
    )

    for !stop {
        char := self.read()
        switch {
        case isLowerCaseLetter(char):
            buf.WriteRune(char)
        case char == eof:
            stop = true
        default:
            self.unread()
            stop = true
        }
    }

    lit := buf.String()
    switch lit {
    case "null":
        return &Token{NULL, lit, bytes.Runes(buf.Bytes())}, nil
    case "true":
        return &Token{TRUE, lit, bytes.Runes(buf.Bytes())}, nil
    case "false":
        return &Token{FALSE, lit, bytes.Runes(buf.Bytes())}, nil
    default:
        return &Token{ILLEGAL, lit, bytes.Runes(buf.Bytes())}, nil
    }
}

开发者ID:gobwas,项目名称:json.go,代码行数:31,代码来源:lexer.go